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

Discuter à nouveau des erreurs courantes en javascript et des méthodes de résolution

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

First: using double quotes all over causes 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, compared with the video, it uses 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 long...Here is the link http://www.cnblogs.com/chinabc/archive/2010/11/19/1881947.html

Second: incorrect addition of semicolons

<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 from toYellow() and toRed(), the code executes normally

Quatrièmement : modification de l'attribut checked des checkbox

Implémenter le tout sélectionner, désélectionner et inverser des checkbox avec trois boutons.

!DOCTYPE html 
<html> 
  <head> 
    <meta charset="UTF-8"> 
    <title></title> 
  </head> 
  <body> 
    <button id="btn">Tout sélectionner</button> 
    <button id="nobtn">Tout désélectionner</button> 
    <button id="inverse">Inverser</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>

La présente contribution sur les erreurs courantes de JavaScript et leurs solutions est tout ce que je partage avec vous aujourd'hui. J'espère que cela vous servira de référence et que vous continuerez à soutenir le tutoriel de cri.

Vous pourriez aussi aimer