js实现给网页换背景this实现
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
body{
/* background: yellow; */
}
</style>
<script type="text/javascript">
window.onload = function(){
var btn = document.getElementsByTagName('button');
// console.log(btn[1].name);
for(var i = 0; i < btn.length; i++){
btn[i].index = i;
btn[i].onclick = function(){
document.body.style.background = btn[this.index].name;
}
}
}
</script>
</head>
<body>
<button type="button" name="yellow">黄色</button>
<button type="button" name="red">红色</button>
<button type="button" name="pink">粉色</button>
</body>
</html>
本文来自博客园,作者:JackieDYH,转载请注明原文链接:https://www.cnblogs.com/JackieDYH/p/17634832.html