< select>中的字体预览下拉列表? [英] Font Previews in <select> dropdown list?

查看:138
本文介绍了< select>中的字体预览下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找,而在下拉列表中,如何使字体预览(在同一个字体中显示文本)真的没有太多。有人可以指出我正确的方向吗?谢谢。

解决方案

提供仅使用的选项 a select 元素,那么允许使用字体样式,并应用其他CSS,同时更新传统的选择元素以提交给服务器/脚本:



html:



 < form action =#method =post> 
< select id =fontSelectorname =fontSelector>
< option value =calibri> Calibri< / option>
< option value =Times New Roman> Times New Roman< / option>
< option value =Comic Sans MS> Comic Sans MS< / option>
< / select>
< / form>

< ul id =fontList>
< li id =calibriclass =selected> Calibri< / li>
< li id =timesNewRoman> Times New Roman< / li>
< li id =comicSansMS> Comic Sans MS< / li>
< / ul>



jQuery:



 code $ $('#fontList li')。click(
function(){
var selected = $(this).index();
$('#fontSelector option :select')
.removeAttr('selected');
$('#fontSelector选项')
.eq(selected)
.attr('selected',true) ;
$('。selected')。removeClass('selected');
$(this).addClass('selected');
});

JS小提琴演示



注意:


  1. li 元素的顺序将与选项元素的顺序相同,因为选择选择由索引( li )选择。 li>

参考文献:


  1. click()

  2. index()

  3. removeAttr()

  4. eq()

  5. < a href =http://api.jquery.com/attr/> attr()

  6. removeClass()

  7. addClass()


I've been looking around and there's really not much on how to make font previews (to show the text in the same font they're selecting) in a dropdown list. Can anybody point me in the right direction? Thanks.

解决方案

To offer an alternative to using only a select element, that will allow for styling the font, and applying other css, while updating a traditional select element for submission to the server/script:

html:

<form action="#" method="post">
    <select id="fontSelector" name="fontSelector">
        <option value="calibri">Calibri</option>
        <option value="Times New Roman">Times New Roman</option>
        <option value="Comic Sans MS">Comic Sans MS</option>
    </select>
</form>

<ul id="fontList">
    <li id="calibri" class="selected">Calibri</li>
    <li id="timesNewRoman">Times New Roman</li>
    <li id="comicSansMS">Comic Sans MS</li>
</ul>

jQuery:

$('#fontList li').click(
    function(){
        var chosen = $(this).index();
        $('#fontSelector option:selected')
            .removeAttr('selected');
        $('#fontSelector option')
            .eq(chosen)
            .attr('selected',true);
        $('.selected').removeClass('selected');
        $(this).addClass('selected');
    });

JS Fiddle demo.

Notes:

  1. This assumes that that the order of the li elements will be the same as the order of the option elements, since the option that becomes selected is chosen by index (of the li).

References:

  1. click().
  2. index().
  3. removeAttr().
  4. eq().
  5. attr().
  6. removeClass().
  7. addClass().

这篇关于&lt; select&gt;中的字体预览下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆