2013-10-25 69 views
-1

我对JS很新,只是写了一些脚本来学习。有人能告诉我我要去哪里吗?我认为这可能只是某处的语法错误,或者我没有使用正确的功能来完成此任务。JavaScript html编辑错误

谢谢:)

HTML:

<!DOCTYPE html> 
<html> 
<head> 
<title>javascript &#8226; training</title> 
<link rel="stylesheet" type="text/css" href="style.css"> 
</head> 

<body> 
    <div id='textOff'> 
     Hi there this is some sample text for my JS 
    </div> 
    <input type='submit' value='show me some stuff!' onclick='show();'/> 
    <script> 
    function show() { 
     var text = document.getElementByID('textOff'); 
     console.log(text); //debugging 
     text.id = 'mainText'; 
    }; 
    </script> 
</body> 

</html> 

CSS:

body { 
    background-color: #17161F; 
    color: white; 
} 

#mainText { 
    width: 20%; 
    height: 30%; 
    font-family: Arial; 
    margin-left: 20%; 
    margin-top: 20%; 
} 

#textOff { 
    display: none; 
} 
+0

那么,什么应该发生和不发生? – pax162

+0

您可能不会注意任何事情,因为1:请参阅下面的关于区分大小写的答案和2:您正在更改元素的“ID”。 – putvande

+3

检查您的错字: 正确的语法是“getElementById”。 – Rayon

回答

6

JavaScript是区分大小写的。

getElementById // correct 
getElementByID // incorrect 

使用浏览器提供的JavaScript控制台:

TypeError: Object #<HTMLDocument> has no method ' getElementByID '

+2

+1斑点! –

+0

非常感谢:) – user2919631

0

使用JavaScript Console在JavaScript中检测到的错误。

这会标记为getElementByID是空引用。

0

浏览器控制台本身告诉有错误:

Uncaught TypeError: Object #<HTMLDocument> has no method 'getElementByID' 

更换getElementByIDgetElementById

function show() { 
     var text = document.getElementById('textOff'); 
     console.log(text); //debugging 
     text.id = 'mainText'; 
    }; 

这里的工作演示:http://jsfiddle.net/fh6NG/