我的DOM如下所示:

<div id="d1">
   <div class="c1">
            <a href="#"><img src="img1_on.gif"></a>
            <a href="#"><img src="img2_on.gif"></a>
   </div>
</div>

当有人单击图像时,我希望图像src更改为<img src=“imgx_off.gif”>,其中x表示图像编号1或2。

这是可能的还是我必须使用CSS来更改图像?


当前回答

无法使用CSS更改图像源。

唯一可能的方法是使用Javascript或任何类似jQuery的Javascript库。

逻辑-

图像位于一个div中,并且该图像没有类或id。

因此,逻辑将是选择图像所在的div中的元素。

然后使用循环选择所有图像元素,并使用Javascript/jQuery更改图像src。

带有演示输出的示例代码-

$(文档).ready(函数){$(“button”).click(函数){$(“#d1.c1 a”).each(函数){$(this).children('img').attr('src','https://www.gravatar.com/avatar/e56672acdbce5d9eda58a178ade59ffe');});});});<script src=“https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js“></script><div id=“d1”><div class=“c1”><a href=“#”><img src=“img1_on.gif”></a><a href=“#”><img src=“img2_on.gif”></a></div></div><button>更改图像</button>

其他回答

这是在Vanilla(或简单的Pure)JavaScript中完成任务的保证方法:

var picurl = 'pictures/apple.png';
document.getElementById("image_id").src=picurl;

我在尝试调用“重新注册”按钮时遇到了同样的问题。经过一些搜索,现在以下功能在几乎所有著名的浏览器(chrome、Firefox、IE、Edge…)中都能正常工作:

function recaptcha(theUrl) {
  $.get(theUrl, function(data, status){});
  $("#captcha-img").attr('src', "");
  setTimeout(function(){
       $("#captcha-img").attr('src', "captcha?"+new Date().getTime());
  }, 0);
 }

“theUrl”用于呈现新的captcha图像,在您的情况下可以忽略。最重要的一点是生成新的URL,迫使FF和IE重新阅读图像。

我在这里做了一个完全具有这种功能的代码笔。我也会在这里给你一个代码的分解。

代码笔

$(函数){//倾听女孩按钮的点击$('#girl btn').click(函数){//单击女孩按钮后,将#square图像的源更改为女孩PNG$('#square').prop(“src”,“https://homepages.cae.wisc.edu/~ece533/images/girl.png“);});//聆听飞机按钮的点击$(“#plane btn”).click(函数){//单击平面按钮后,将#square图像的源更改为平面PNG$('#square').prop(“src”,“https://homepages.cae.wisc.edu/~ece533/images/airplane.png“);});//聆听水果按钮的点击$(“#fruits btn”).click(函数){//单击水果按钮后,将#square图像的源更改为水果PNG$('#square').prop(“src”,“https://homepages.cae.wisc.edu/~ece533/images/fruits.png“);});});<script src=“https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js“></script><img src=“https://homepages.cae.wisc.edu/~ece533/images/girl.png“id=”square“/><div><button id=“girl btn”>女孩</button><button id=“plane btn”>平面</button><button id=“fruits btn”>水果</button><a href=“https://homepages.cae.wisc.edu/~ece533/images/“>图像来源</a></div>

当改变图像源时,人们会犯的一个常见错误是不等待图像加载来执行后续操作,如成熟的图像大小等。在图像加载之后,您需要使用jQuery.load()方法来执行操作。

$('yourimageselector').attr('src', 'newsrc').load(function(){
    this.width;   // Note: $(this).width() will not work for in memory images

});

编辑原因:https://stackoverflow.com/a/670433/561545

如果不仅有jQuery或其他资源清除框架-每个用户每次都要下载很多kb,只是为了一个简单的技巧-而且还有原生JavaScript(!):

<img src="img1_on.jpg" 
    onclick="this.src=this.src.match(/_on/)?'img1_off.jpg':'img1_on.jpg';">
<img src="img2_on.jpg" 
    onclick="this.src=this.src.match(/_on/)?'img2_off.jpg':'img2_on.jpg';">

这可以写得一般而优雅:

<html>
<head>
<script>
function switchImg(img){
    img.src = img.src.match(/_on/) ? 
        img.src.replace(/_on/, "_off") : 
        img.src.replace(/_off/, "_on");
}
</script>
</head>
<body>
    <img src="img1_on.jpg" onclick="switchImg(this)">
    <img src="img2_on.jpg" onclick="switchImg(this)">
</body>
</html>