关于网页 动态替换(简单)
今天小编 尝试着写了一个 关于 网页动态修改图片的代码,略微麻烦,但是确实很简单。
首先,我想要做的是就是 点击网页中的 修改按钮,然后下放的图片就会按照某个顺序及逆行自动的替换(相当于一个死循环),不过,我觉得 死循环 太浪费 内存空间了。
所以 我在 script 内部 定义了一个全局变量,用来表示 点击得次数,小编 总共放上去了 4 张土拍你,所以 小编 通过点击次数 对于 4 得余数 当作索引,进行相应得图片得选择和替换。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>循环学习</title>
<style>
button{
background-color: cornflowerblue;
font-style: oblique;
font-family: 宋体;
font-size: 20px;
}
div{
background-image: url("../image/美女3.jpg");
border: double 5px blueviolet;
width: 500px;
height: 500px;
background-position: center;
}
span{
font-size: 30px;
color: cadetblue;
font-family: Bahnschrift;
}
</style>
</head>
<body>
<span>点击按钮,更换图片</span>
<button id="btn">修改</button>
<div id="div">
</div>
<script>
var a=0;
var c=['git.jpg','孙悟空.jpg','美女.jpeg','美女2.jpeg', '美女3.jpg'];
var btn = document.getElementById('btn');
var div = document.getElementById('div')
btn.onmouseenter = function () {
btn.style.backgroundColor = 'aquamarine';
btn.style.fontSize = '30px';
}
btn.onmouseleave = function () {
btn.style.backgroundColor = 'cornflowerblue';
btn.style.fontSize = '20px';
}
btn.onclick = function () {
var b;
b = a % 4;
a++;
var d = 'url("../image/ ")';
var e;
e = d.replace(' ', c[b]);
div.style.backgroundImage = e;
}
</script>
</body>
</html>