日期:2014-05-16  浏览次数:20408 次

初学js,问一个简单js代码,大家给看看??(总提示document.all(...)为空或不是对象,我想选1出层1,选2出层2)
<html>
<head>
<meta   http-equiv= "Content-Type "   content= "text/html;   charset=gb2312 ">
<title> 无标题文档 </title>
</head>
<script   language= "jscript ">
function   change(selectIndex)
{  
  chosenLayer= "nr_ "+selectIndex;
  document.all(chosenLayer).visibility= "show ";    
}
function   errortrap(msg,url,line){
alert(msg);
return   true;
}
onerror=errortrap;
</script>
<body   >
<layer   name= "nr_0 "   visibility= "hide ">
<TABLE> <TR> <TD> LAYER0 </TD> </TR> </TABLE>
</layer>
<layer   name= "nr_1 "   visibility= "hide ">
<TABLE> <TR> <TD> LAYER1 </TD> </TR> </TABLE>
</layer>

<form>
<select   name= "sel "   onChange= "change(this.selectedIndex) ">
<option   > 0 </option>
<option   > 1 </option>
</select> </form>
  </body>
</html>


------解决方案--------------------
请使用 document.getElementById
------解决方案--------------------
name不被all识别 要用id
在layer中加上id= "nr_0 "

------解决方案--------------------
onchange响应事件应该写成onChange= "change(this.selectedIndex.value) "
------解决方案--------------------
<select name= "sel " onChange= "change(this.selectedIndex) "> 是取不到值的
写成 this.options[this.selectedIndex].value
还有document.all是取id值的 <layer name= "nr_0 "改为 <layer id= "nr_0 "


------解决方案--------------------
在Layer或Div中加ID,设置sytle= "display:none "
是1的时候
LayerID.style.dispaly= "inline "
建议