2013-10-25 68 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/