<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<button type="button" id="button_A">changeBackground</button>
<script type="text/javascript">
function logo() {
var btn = document.getElementById("button_A")
i = 0
var imgArr = ["img/1.jpg", "img/2.jpg", "img/3.jpg", "img/4.jpg", "img/5.jpg", "img/6.jpg"]
btn.addEventListener("click", function () {
if (i == 6) {
i = 0
}
document.body.style.backgroundImage = "url('"+imgArr[i]+"')"
i++
})
}
logo()
</script>
</body>
</html>
用您的改进后的代码,js代码必须再按钮的标签后面(要不然需要再监听dom加载完毕后执行js),i不应该一直加下去,会出错!
参考:http://www.w3school.com.cn/jsref/prop_style_backgroundimage.asp
<script type="text/javascript">function change() {
document.getElementById("img").src = "../../Styles/icons/help2.png"
}
</script>
<img id="img" onclick="change()" src="../../Styles/icons/help.png"/>
//img 本身有一个点击事件 不需要服务器控件,用js就能实现了!