<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用this传递当前对象本身</title>
<meta charset="utf-8" />
</head>
<body>
<img src="usa.gif" onclick="changeImg(this)" alt=""/>
<img src="mexico.gif" onclick="changeImg(this)" alt=""/> <script>
var myImages = [
"usa.gif",
"canada.gif",
"jamaica.gif",
"mexico.gif"
]; function changeImg(that) {
var newImgNumber = Math.round(Math.random() * 3); //确保随机生成的图片索引不和当前的一样
while (that.src.indexOf(myImages[newImgNumber]) != -1) {
newImgNumber = Math.round(Math.random() * 3);
} that.src = myImages[newImgNumber];
}
</script>
</body>
</html>