我刚刚开始学习JavaScript,无法弄清楚为什么getElementById不起作用?
<!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>
Run Code Online (Sandbox Code Playgroud)
这个想法是,一旦用户点击摇滚,询问他的名字,就会问候他并改变图像.名称/问候语部分有效,但图像不会改变.
有任何想法吗?谢谢!