English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Further Discussion on Common JavaScript Errors and Solutions

As a beginner in Javascript, I am always tortured by small problems every day, and tonight I have several small problems.

First: using double quotes all over caused matching errors

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

The line has been reporting an error all the time: unexpected token "}" Check for half a day and couldn't find any error, found that the video is using single quotes

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

After changing to single quotes, the error was finally eliminated, which troubled me all night...Here is the link http://www.cnblogs.com/chinabc/archive/2010/11/19/1881947.html

Second: incorrectly added semicolon

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

An extra semicolon was written, causing the code after the semicolon not to execute

Third: too many parentheses after the function name

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

After removing the parentheses after toYellow() and toRed(), the code executes normally

Fourth: Modify the checked attribute of checkbox

Implement the full selection, all selection, and reverse selection of checkbox with three buttons.

<!DOCTYPE html> 
<html> 
  <head> 
    <meta charset="UTF-8"> 
    <title></title> 
  </head> 
  <body> 
    button>/button> 
    <button id="nobtn">Select All</button> 
    <button id="inverse">Reverse Selection</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; 
          } 
            input[i].checked=false; 
          } 
        } 
      } 
    </script> 
  </body> 
</html>

This article discusses common JavaScript errors and solutions, which is all the editor has shared with everyone. It is hoped that this can provide a reference for everyone and that everyone will support the Yell Tutorial more.

You May Also Like