再談javascript常見錯誤及解決方案_javascript技巧

來源:互聯網
上載者:User

初學Javascript,每天總是被很小的問題折磨半天,今晚就有好幾個小問題。

第一:全部使用雙引號造成匹配錯誤

<input type="checkbox" onmouseover="document.getElementById("test").style.display="none":"/>

改行一直報錯誤:unexpected toke “}”  檢查半天也沒有發現錯誤,對照發現視頻上是使用單引號

<input type="checkbox" onmouseover="document.getElementById('test').style.display="none":"/> 

改成單引號後錯誤總算消除,困擾我一夜晚。。附上連結http://www.cnblogs.com/chinabc/archive/2010/11/19/1881947.html

第二:錯誤添加分號

<div id="test" class="test1" onmouseover="toYellow()" ;onmouseout="toRed()";>change</div> 

多寫了一個分號,導致分號後的代碼不執行

第三:函數名後多寫括弧

<script>   function toYellow(){     document.getElementById("test").className="test2";     }   function toRed(){      document.getElementById("test").className="test1";     }   document.getElementById("test").onmouseover=toYellow();   document.getElementById("test").onmouseout=toRed(); </script> 

卻掉 toYellow()和 toRed()後的括弧後正常執行

第四:checkbox的checked屬性修改

用三個按鈕實現checkbox的全選、全部選、反選。

<!DOCTYPE html> <html>   <head>     <meta charset="UTF-8">     <title></title>   </head>   <body>     <button id="btn">全選</button>     <button id="nobtn">全不選</button>     <button id="inverse">反選</button><br />     <input type="checkbox" />     <input type="checkbox" />     <input type="checkbox" />     <input type="checkbox" />     <input type="checkbox" />     <input type="checkbox" />     <input type="checkbox" />     <script>       var btn=document.getElementById("btn");       var input=document.getElementsByTagName("input");       btn.onclick=function(){         for(var i=0;i<input.length;i++){           input[i].checked="checked";         }       }       var nobtn=document.getElementById("nobtn");       nobtn.onclick=function(){         for(var i=0;i<input.length;i++){           input[i].checked=false;         }       }       var inverse=document.getElementById("inverse");       inverse.onclick=function(){         for(var i=0;i<input.length;i++){           if(input[i].checked==false){             input[i].checked=true;           }else{             input[i].checked=false;           }         }       }     </script>   </body> </html>

以上這篇再談javascript常見錯誤及解決方案就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.