2015-03-08 136 views
-2

我在输出中得到的所有内容都是changeColor未定义,changeAgain未定义。我试图解决它,但它似乎并没有工作。任何帮助将不胜感激,我已到处看!JavaScript/HTML错误的解决方案

这里是我的代码

<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <meta charset="utf-8"> 
 
    <title>JS Bin</title> 
 
</head> 
 

 
<body onLoad="alert('Website Loaded');"> 
 

 
    <script language="javascript" type="text/javascript"> 
 
    function changeColor() { 
 
     document.GetElementById("h3style").style.color = "red"; 
 
     document.GetElementById("h3style").firstChild.nodeValue = "RED!"; 
 
     return true; 
 
    } 
 

 
    function changeAgain() { 
 
     document.GetElementById("h3style").style.color = "gray"; 
 
     document.GetElementById("h3style").firstChild.nodeValue = "Gray..."; 
 
     return true; 
 
    } 
 
    </script> 
 
    <noscript> 
 
    <h1>THIS CONTENT REQUIRES JAVASCRIPT | PLEASE ENABLE JAVASCRIPT</h1> 
 
    </noscript> 
 

 

 
    <h3 id="h3style" onMouseOver="changeColor()" onMouseOut="changeAgain()">Scroll on me</h3> 
 

 
</body> 
 

 
</html>

回答

1

它应该是getElementById

Check docs here

<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <meta charset="utf-8"> 
 
    <title>JS Bin</title> 
 
</head> 
 

 
<body onLoad="alert('Website Loaded');"> 
 

 
    <script language="javascript" type="text/javascript"> 
 
    function changeColor() { 
 
     document.getElementById("h3style").style.color = "red"; 
 
     document.getElementById("h3style").firstChild.nodeValue = "RED!"; 
 
     return true; 
 
    } 
 

 
    function changeAgain() { 
 
     document.getElementById("h3style").style.color = "gray"; 
 
     document.getElementById("h3style").firstChild.nodeValue = "Gray..."; 
 
     return true; 
 
    } 
 
    </script> 
 
    <noscript> 
 
    <h1>THIS CONTENT REQUIRES JAVASCRIPT | PLEASE ENABLE JAVASCRIPT</h1> 
 
    </noscript> 
 

 

 
    <h3 id="h3style" onMouseOver="changeColor()" onMouseOut="changeAgain()">Scroll on me</h3> 
 

 
</body> 
 

 
</html>

+0

三江源。我知道它是一个简单的东西,但即时启动JavaScript! – notBillGates 2015-03-08 19:04:40

+0

不客气。我很高兴它有帮助。 https://developer.mozilla.org/en-US/是一个很好的资源。如果这是您正在寻找的解决方案,请将其标记为已回答。 – Dhiraj 2015-03-08 19:22:37