更改选择标签中单独选项的 css font-family
我不知道这是否可能,如果不可能,是否有人可以提出可选的想法,但我试图在选择标签中显示不同字体的下拉菜单(特别是来自 Google 字体目录的字体)。在下拉列表中,我尝试通过使用每个选项所代表的字体设置样式来显示预览,
<option name="Tangerine" style="font-family:'Tangerine', verdana;">Tangerine</option>
但这似乎不起作用。有任何线索为什么或如何解决它吗?
I don't know if this is possible, and if not, if someone can throw out optional ideas, but I'm attempting to display a drop down of different fonts (specifically, font's from Google's font directory) in a select tag. In the drop down, I'm trying to show a preview by styling each option with the font it represents
<option name="Tangerine" style="font-family:'Tangerine', verdana;">Tangerine</option>
This doesn't seem to be working, though. Any clues why or how to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
上面提到的所有选项在 OSX Safari 上都无法正确显示。我设法通过使用引导程序使事情正确:
All options mentioned above didnt display thing correct on OSX Safari. I managed to get things correct by using bootstrap:
您应该将每个
option
标签包装在optgroup
标签中,然后将其样式设置为:You should wrap each
option
tag in anoptgroup
tag and then style that as:您无法设置
标记的字体,但您可以创建具有特定样式的列表(如您所期望的)
以下是我一直在尝试的所有内容:
You cannot set the font of the
<option>
tag but you could create a list with specific styles (as you would expect)Here's everything I've been trying:
也许你可以使用javascript?你能试试这个吗?
Maybe you can use javascript? Can you try this ?
你可以像这样简单地使用
You can simply use like this
将字体名称赋予选项值。然后你可以用jquery来设置它们。
这是一个示例:
您可以像这样轻松添加新字体:
Give the font name to option value. Then you can set them all with jquery.
Here is an example:
You can add new font easily like this:
我使用了一种 hackish 风格的方法来做到这一点。
因为我需要设置 select 标签的样式并想要自定义更多内容,所以我使用了 jQuery 的 Stylish Select 插件,并将 font-family css 属性附加到父元素(这是一个列表标签)。
只需很少的代码即可运行,并且跨浏览器兼容。
I used a hackish-style method to do it.
Because I needed to style the select tag and wanted to customize a couple more things, I used the Stylish Select plugin for jQuery and attached the font-family css attribute to the parent element (which was a list tag).
That worked with little code and was cross-browser compatible.