js替换div下的所有图片为一张通用图片,点击图片又显示回相对应的图片
1个回答
展开全部
<div id="mochu">
<p>内容</p>
<p><img src="samples/1.jpg"></p>
<p><img src="samples/2.jpg"></p>
<p><img src="samples/3.jpg"></p>
<p><button onclick="change_default();">替换</button></p>
</div>
<script type="text/javascript">
function change_default(){
var children = document.getElementById('mochu').querySelectorAll("img");
for (var i = 0; i < children.length; i++) {
var old_src = "change_img(this, '" + children[i].src + "');";
children[i].parentNode.innerHTML = '<img src="samples/4.jpg" onclick="' + old_src + '">';
}
}
function change_img(obj, old_src){
obj.src = old_src;
}
</script>
<p>内容</p>
<p><img src="samples/1.jpg"></p>
<p><img src="samples/2.jpg"></p>
<p><img src="samples/3.jpg"></p>
<p><button onclick="change_default();">替换</button></p>
</div>
<script type="text/javascript">
function change_default(){
var children = document.getElementById('mochu').querySelectorAll("img");
for (var i = 0; i < children.length; i++) {
var old_src = "change_img(this, '" + children[i].src + "');";
children[i].parentNode.innerHTML = '<img src="samples/4.jpg" onclick="' + old_src + '">';
}
}
function change_img(obj, old_src){
obj.src = old_src;
}
</script>
追问
如何不做按钮,就只是点击图片
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询