更改选择标签中单独选项的 css font-family

发布于 2024-10-11 20:33:04 字数 273 浏览 3 评论 0原文

我不知道这是否可能,如果不可能,是否有人可以提出可选的想法,但我试图在选择标签中显示不同字体的下拉菜单(特别是来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

南七夏 2024-10-18 20:33:05

上面提到的所有选项在 OSX Safari 上都无法正确显示。我设法通过使用引导程序使事情正确:

<!DOCTYPE html>
    <html lang="nl">
    <head>
    <meta charset="utf-8" />
    <title>font test</title>
    <meta name="generator" content="BBEdit 10.5" />
    <link rel="stylesheet" type="text/css" href="yourpaththere/css/bootstrap.min.css" async />
    </head>
    <body>
    <style>
    @font-face {font-family: edwardian;src:local("edwardian"),url('yourpathhere/font/edwardian.TTF');font-weight: normal;}
    .edwardian{font-family: edwardian; font-size: 30px;}

    .arial{font-family: arial;}
    .times{font-family: times;}
    .verdana{font-family: verdana;}
    .helvetica{font-family: helvetica;}
    </style>
    <div id="fontdropdown1" class="dropdown" style="width: 300px;">
    <button class="btn btn-default dropdown-toggle" type="button" id="menu1" data-toggle="dropdown">Kies een lettertype
    <span class="caret"></span></button>
    <ul class="dropdown-menu" role="menu" aria-labelledby="menu1">
    <li role="presentation"><a role="menuitem" class="arial">Arial</a></li>
    <li role="presentation" class="divider"></li>
    <li role="presentation"><a role="menuitem" class="times">Times</a></li>
    <li role="presentation"><a role="menuitem" class="verdana">Verdana</a></li>
    <li role="presentation"><a role="menuitem" class="helvetica">Helvetica</a></li>
    <li role="presentation"><a role="menuitem" class="edwardian">Edwardian</a></li>
    </ul>
    </div>
    <!-- test font dropdown -->
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
    <script type="text/javascript" src="yourpathhere/js/jquery-1.11.3.min.js"></script>
    <script type="text/javascript" src="yourpathhere/js/bootstrap.js"></script>
    </body>
    </html>

All options mentioned above didnt display thing correct on OSX Safari. I managed to get things correct by using bootstrap:

<!DOCTYPE html>
    <html lang="nl">
    <head>
    <meta charset="utf-8" />
    <title>font test</title>
    <meta name="generator" content="BBEdit 10.5" />
    <link rel="stylesheet" type="text/css" href="yourpaththere/css/bootstrap.min.css" async />
    </head>
    <body>
    <style>
    @font-face {font-family: edwardian;src:local("edwardian"),url('yourpathhere/font/edwardian.TTF');font-weight: normal;}
    .edwardian{font-family: edwardian; font-size: 30px;}

    .arial{font-family: arial;}
    .times{font-family: times;}
    .verdana{font-family: verdana;}
    .helvetica{font-family: helvetica;}
    </style>
    <div id="fontdropdown1" class="dropdown" style="width: 300px;">
    <button class="btn btn-default dropdown-toggle" type="button" id="menu1" data-toggle="dropdown">Kies een lettertype
    <span class="caret"></span></button>
    <ul class="dropdown-menu" role="menu" aria-labelledby="menu1">
    <li role="presentation"><a role="menuitem" class="arial">Arial</a></li>
    <li role="presentation" class="divider"></li>
    <li role="presentation"><a role="menuitem" class="times">Times</a></li>
    <li role="presentation"><a role="menuitem" class="verdana">Verdana</a></li>
    <li role="presentation"><a role="menuitem" class="helvetica">Helvetica</a></li>
    <li role="presentation"><a role="menuitem" class="edwardian">Edwardian</a></li>
    </ul>
    </div>
    <!-- test font dropdown -->
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
    <script type="text/javascript" src="yourpathhere/js/jquery-1.11.3.min.js"></script>
    <script type="text/javascript" src="yourpathhere/js/bootstrap.js"></script>
    </body>
    </html>
野稚 2024-10-18 20:33:04

您应该将每个 option 标签包装在 optgroup 标签中,然后将其样式设置为:

<select>

  <optgroup style="font-family:arial">
    <option>Arial</option>
  </optgroup>

  <optgroup style="font-family:verdana">
    <option>veranda</option>
  </optgroup>

  <optgroup style="font-family:other">
    <option>other</option>
  </optgroup>

</select>

You should wrap each option tag in an optgroup tag and then style that as:

<select>

  <optgroup style="font-family:arial">
    <option>Arial</option>
  </optgroup>

  <optgroup style="font-family:verdana">
    <option>veranda</option>
  </optgroup>

  <optgroup style="font-family:other">
    <option>other</option>
  </optgroup>

</select>

墨离汐 2024-10-18 20:33:04

您无法设置 标记的字体,但您可以创建具有特定样式的列表(如您所期望的)

以下是我一直在尝试的所有内容:

$("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");
h1 {
  font-size: 1.2em;
  margin-top: 10px;
  color: #777;
}
.tangerine {
  font-family: tangerine;
}
.tahoma {
  font-family: tahoma;
}
.timesnewroman {
  font-family: times new roman;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<h1>Set with jQuery</h2>
<select id="js">
    <option>Tangerine</option>
    <option>Tahoma</option>
    <option>Times New Roman</option>
</select>
<div> </div>
<h1>Set with CSS</h2>
<select id="css">
    <option class="tangerine">Tangerine</option>
    <option class="tahoma">Tahoma</option>
    <option class="timesnewroman">Times New Roman</option>
</select>
<div> </div>
<h1>List</h2>
<ul>
    <li>Tangerine</li>
    <li>Tahoma</li>
    <li>Times New Roman</li>
</ul>

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:

$("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");
h1 {
  font-size: 1.2em;
  margin-top: 10px;
  color: #777;
}
.tangerine {
  font-family: tangerine;
}
.tahoma {
  font-family: tahoma;
}
.timesnewroman {
  font-family: times new roman;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<h1>Set with jQuery</h2>
<select id="js">
    <option>Tangerine</option>
    <option>Tahoma</option>
    <option>Times New Roman</option>
</select>
<div> </div>
<h1>Set with CSS</h2>
<select id="css">
    <option class="tangerine">Tangerine</option>
    <option class="tahoma">Tahoma</option>
    <option class="timesnewroman">Times New Roman</option>
</select>
<div> </div>
<h1>List</h2>
<ul>
    <li>Tangerine</li>
    <li>Tahoma</li>
    <li>Times New Roman</li>
</ul>

鹤仙姿 2024-10-18 20:33:04

也许你可以使用javascript?你能试试这个吗?

<script>
myObj = document.getElementById("tahoma");
myObj.style.font = "Tahoma";
myObj.style.color = "red";

// Change anything else that you like!
</script>

<option id="tahoma .....>

Maybe you can use javascript? Can you try this ?

<script>
myObj = document.getElementById("tahoma");
myObj.style.font = "Tahoma";
myObj.style.color = "red";

// Change anything else that you like!
</script>

<option id="tahoma .....>
錯遇了你 2024-10-18 20:33:04

你可以像这样简单地使用

<select id='lstFonts' class='form-control'>
    <option style="font-family:Arial" value="Arial">Arial</option>
    <option style="font-family:Verdana" value="Verdana">Arial</option>
</select>

You can simply use like this

<select id='lstFonts' class='form-control'>
    <option style="font-family:Arial" value="Arial">Arial</option>
    <option style="font-family:Verdana" value="Verdana">Arial</option>
</select>
白色秋天 2024-10-18 20:33:04

将字体名称赋予选项值。然后你可以用jquery来设置它们。

这是一个示例:

您可以像这样轻松添加新字体:

<option value="myFontName">My Font Name</option>

$("#selectfont").each(function(){
  $(this).children("option").each(function(){
    $(this).css("fontFamily",this.value);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<select id="selectfont">
<option value="tangerine">Tangerine</option>
<option value="tahoma">Tahoma</option>
<option value="times new roman">Times New Roman</option>
</select>

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:

<option value="myFontName">My Font Name</option>

$("#selectfont").each(function(){
  $(this).children("option").each(function(){
    $(this).css("fontFamily",this.value);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<select id="selectfont">
<option value="tangerine">Tangerine</option>
<option value="tahoma">Tahoma</option>
<option value="times new roman">Times New Roman</option>
</select>

云裳 2024-10-18 20:33:04

我使用了一种 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文