开发者

getElementById won't work [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 9 years ago.

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>&nbsp;</p>
<p>&nbsp;</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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜