sup*_*er9 -1 javascript if-statement
我正在搞乱Head First Javascript书.所以我的问题是为什么if
我的函数中的子句不会turnSad()
触发?更新我在Chrome和FF4中尝试过这个.另外,如果我要对if
语句进行注释,该函数将起作用.
<script type="text/javascript">
function turnSad() {
if (document.getElementById("rockImg").src == "rock_happy.png")
document.getElementById("rockImg").src = "rock.png";
}
function touchRock() {
var userName = prompt("What is your name?", "Enter your name here");
if (userName) {
alert("It is nice to meet you " + userName + ".");
document.getElementById("rockImg").src = "rock_happy.png";
}
setTimeout("turnSad();", 1000);
}
</script>
<img id="rockImg" src="rock.png" alt="iRock" style="cursor:pointer"
onclick="touchRock();" />
Run Code Online (Sandbox Code Playgroud)
当访问src
属性(不是属性)时,浏览器可以自动解析相对路径并返回绝对路径,包括协议,域,端口等.
要获取或更改属性值,请使用getAttribute
和setAttribute
.
image.getAttribute("src");
image.setAttribute("src", "someImage.png");
Run Code Online (Sandbox Code Playgroud)
看这个例子.