getElementById won't work [closed]
I'm just starting to learn JavaScript and can't figure out why getElementById won't work?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The Rock</title>
<style type="text/css">
<!--
body {
text-align: center;
}
-->
</style>
<script type="text/javascript">
function touchRock() {
var username = prompt("What is your name?");
if (username) { alert("Hello, "+username+"! I am The Rock.");
document.getElementById("rockimg").scr = "rock2.png";}
}
</script>
</head>
<body>
开发者_运维技巧<p> </p>
<p> </p>
<p><img id="rockimg" src="rock1.png" align="middle" style="cursor:pointer" onclick="touchRock();" /></p>
</body>
</html>
The idea is that once the user click on the rock, asks for his name, greets him and changes the image. The name/greeting part works, but the image doesn't change.
Any ideas? Thanks!
You got a typo.
Change .scr
into .src
and it will work
You have a typo on this line:
document.getElementById("rockimg").scr
Should be src
not scr
change
document.getElementById("rockimg").scr
to
document.getElementById("rockimg").src
(it's src, not scr)
精彩评论