Change css font-family for separate options in select tag
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
This doesn't seem to be working, though. Any clues why or how to fix it?
回答1:
You should wrap each option tag in an optgroup tag and then style that as:
回答2:
You cannot set the font of the tag but you could create a list with specific styles (as you would expect)
Here's everything I've been trying:
$("select.js option:eq(0), ul li:eq(0)").css("fontFamily", "tangerine"); $("select.js option:eq(1), ul li:eq(1)").css("fontFamily", "tahoma"); $("select.js option:eq(2), ul li:eq(2)").css("fontFamily", "times new roman");
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.
回答6:
Give the font name to option value. Then you can set them all with jquery.