【发布时间】:2020-05-16 00:18:04
【问题描述】:
当您从下拉列表中选择要使用的字体时,我正在尝试更改旁边的字体。
但是,我的代码将获取的唯一值是列表的第一个选项。当我单击另一种字体时,什么也没有发生。如何选择其他值?谢谢。
var fontVal = $("select option").attr("value");
alert(fontVal);
function fontChoice(){
if(fontVal == "Choose"){
$('#mirror').css({"font-family":"sans-serif"});
console.log('Choose');
}else if(fontVal == "Arial"){
$('#mirror').css({"font-family":"Arial"});
console.log('Courier');
}else if(fontVal == "Times"){
$('#mirror').css({"font-family":"Times New Roman"});
console.log('Times');
}else if(fontVal == "Impact"){
$('#mirror').css({"font-family":"Impact"});
console.log('Impact');
}else if(fontVal == "Courier"){
$('#mirror').css({"font-family":"Courier"});
console.log('Courier');
};
};
fontChoice();
<aside id="mirror">
</aside>
<select id="fonts">
<option value="Choose">Choose your font</option>
<option value="Arial">Arial</option>
<option value="Times">Times New Roman</option>
<option value="Impact">Impact</option>
<option value="Courier">Courier</option>
</select>
【问题讨论】:
-
试试这个小提琴jsfiddle.net/s9v4mvfv
-
你需要将你的函数绑定到 select 元素的 change 事件上。
标签: jquery