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

Mais uma discussão sobre erros comuns em JavaScript e suas soluções

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 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 "}" I checked 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, it troubled me all night...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 of toYellow() and toRed(), it executes normally

Quarto: modificar a propriedade checked do checkbox

Implementar o controle total de checkbox com três botões.

!DOCTYPE html 
<html> 
  <head> 
    <meta charset="UTF-8"> 
    <title></title> 
  </head> 
  <body> 
    <button id="btn">Marcar todos como selecionados</button> 
    <button id="nobtn">Marcar todos como não selecionados</button> 
    <button id="inverse">Inverter</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>

Com este artigo, falamos sobre os erros comuns do JavaScript e suas soluções, que é tudo o que o editor compartilha com vocês. Esperamos que isso possa ser uma referência para vocês e que vocês possam apoiar e aplaudir o tutorial.

Você também pode gostar