【发布时间】:2017-02-04 18:33:21
【问题描述】:
这可能吗?:
我有:
var titles = ["opt1 (beginner)", "opt2 (intermediate)", "opt3 (prof)"]
var select = document.getElementById('select');
for (i = 0; i < titles.length; i++) {
var option = document.createElement("option");
option.textContent = titles[i];
select.append(option);
}
select.options[select.selectedIndex].innerHTML = "option1"
select.onfocus = function(){
for (i = 0; i < titles.length; i++) {
select.options[i].textContent = titles[i];
}
};
<select id="select"></select>
这向我展示了标题中定义的选项值。
当<select> 处于焦点时,应该向我显示标题中定义的完整值,而当 select 未处于焦点时,我希望它显示不同的东西,所以在这种情况下 option1 而不是 opt1(初学者)。
我尝试添加这个:
select.options[select.selectedIndex].innerHTML = "option1"
select.onfocus = function(){
for (i = 0; i < titles.length; i++) {
select.options[i].textContent = titles[i];
}
};
还尝试了 onchange 事件,但到目前为止没有任何效果。
这是fiddle
【问题讨论】:
-
不要使用外部小提琴,使用Stack Snippets
-
完全正确!,非常感谢,如果您将其发布为答案,我会接受它
标签: javascript html select option