Javascript Question: Misunderstand either getElementbyID or innerHTML
Can anyone tell me why, with the following code, when I click "Click Here", the text doesn't change开发者_如何学Python to "test"?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<div id="thisdiv" onClick="document.getElementbyID('thisdiv').innerHTML='test'">Click Here</div>
</body>
</html>
You have a lowercase b
and an uppercase D
.
//-------v------should be uppercase
getElementById
//----------^---should be lowercase
Example: http://jsfiddle.net/qSEpF/
But simpler would be to reference the current element with this
:
onClick="this.innerHTML='test'"
Example: http://jsfiddle.net/qSEpF/1/
JavaScript is case-sensitive, as Patrick pointed out. And, let's be honest: Intellisense for JavaScript pretty much blows. Be very careful when writing script.
What likely happened is that when you attempted to invoke the method with the wrong case, JavaScript couldn't find it on the inheritance change and created it instead, with the wrong case. That would explain why you got no script error, and nothing happened.
精彩评论