< select>中的字体预览下拉列表? [英] Font Previews in <select> dropdown list?
本文介绍了< 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');
});
注意:
-
li
元素的顺序将与选项
元素的顺序相同,因为选择
选择由索引(li
)选择。 li>
参考文献:
-
click()
。 -
index()
。 -
removeAttr()
。 -
eq()
。 - < a href =http://api.jquery.com/attr/>
attr()
。 -
removeClass()
。 -
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');
});
Notes:
- This assumes that that the order of the
li
elements will be the same as the order of theoption
elements, since theoption
that becomesselected
is chosen by index (of theli
).
References:
这篇关于< select>中的字体预览下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文