刚开始学习js就遇到一个纠结的问题。。如果我复制教材上的代码。。工作正常。。自己写的就各种没反应。!!求解救
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<title>ok</title>
<script type="text/javascript" src="js.js"></script>
</head><body>
<form action="#" >
  <input type="button" id="Nihao" value="你好" /> 
  <input type="button" id="Woqu" value="我好" />
  <input type="button" id="Dajiahao" value="大家好" />
</form>
</body>
</html>
window.onload = initAll;function initAll() {
document.getElementById("Nihao").onclick = chuLi;
document.getElementById("Wohao").onclick = chuLi;
document.getElementById("Dajiahao").onclick = chuLi;
}
function chuLi() {
switch (this,id) {
case "Nihao":
  alert("你好");
  break;
case "Wohao":
  alert("我好");
  break;
case "Dajiahao":
  alert("ssssss");
  break;
default:
}
}
有什么遗漏掉的吗???空格什么的应该没差吧??会不会是函数名的问题??想了半天,没想出来

解决方案 »

  1.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <head>
    <title>ok</title>
    <script>
    window.onload = initAll;
     
    function initAll() {
    document.getElementById("Nihao").onclick = chuLi;
    //document.getElementById("Wohao").onclick = chuLi;
    document.getElementById("Woqu").onclick = chuLi;
    document.getElementById("Dajiahao").onclick = chuLi;
    }
    function chuLi() {
        //switch (this,id) {
        switch (this.id) {
            case "Nihao":
              alert("你好");
              break;
            //case "Wohao":
    case "Woqu":
              alert("我好");
              break;
            case "Dajiahao":
              alert("ssssss");
              break;
            default:
        }
    }
    </script>
    </head>
     
    <body>
    <form action="#" >
      <input type="button" id="Nihao" value="你好" /> 
      <input type="button" id="Woqu" value="我好" />
      <input type="button" id="Dajiahao" value="大家好" />
    </form>
    </body>
    </html>
      

  2.   

    请使用firefox的firebug等浏览器调试工具调试。
      

  3.   

    function chuLi() {
        switch (this.id) { //不是 this,id
            case "Nihao":
              alert("你好");
              break;
            case "Wohao": //不是 Woqu
              alert("我好");
              break;
            case "Dajiahao":
              alert("ssssss");
              break;
            default:
        }
    }
      

  4.   

    二楼回答正确,版主有遗漏,case "Wohao": //不是 Woqu
              alert("我好");
              break;
    楼主的按钮ID就是  woqu!
      

  5.   

     switch (this,id) 
      

  6.   

    这里只是为了说明,楼主语言本身并没有错。。最多id为undefined...
    /首先需要明白,这里有一个东西:
    alert(  (1,2,3,4,5)  ); //将弹出5,逗号是一个运算符。 var a = 1;
        switch ( new Array(), 1,2,3,4,5 ) {  //这里可以弄很多东西,但是逗号仅仅作为一种运算符存在。switch的参数将是里面的运算结果。即最后一个。
    case 1: alert(111); break;
    case 2: alert(222); break;
    case 3: alert(333); break;
    case 4: alert(444); break;
    case 5: alert(555); break;  //这里执行
        }