的JavaScriptJavaScript:如何根据选择框中选择的选项制作div的样式?
function displayContent()
{
var 1 = getElementById(1);
var 2 = getElementById(2);
var 3 = getElementById(3);
var 4 = getElementById(4);
if(document.form.list.selectedIndex==0) {
1.style.display = "block";
}
if(document.form.list.selectedIndex==1) {
2.style.display = "block";
}
if(document.form.list.selectedIndex==2) {
3.style.display = "block";
}
if(document.form.list.selectedIndex==3) {
4.style.display = "block";
}
}
HTML
<form id="form">
<select onchange="displayContent();" id="list">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
</form>
<div id="1">Content1</div>
<div id="2">Content2</div>
<div id="3">Content3</div>
<div id="4">Content4</div>
这是剧本,我已经把标记。在此之前,我尝试了几种不同的方式,但所有这些方法都会导致相同的非解决方案,就像这样。当我在选择框中更改选项时没有任何反应。我错过了什么吗?
默认情况下,所有的div设置为display:none;
。
感谢, 乔
你的ID应该以字母开头,而不是数字 – jbabey