jquery中如何交换图片
如何在单击图像时交换图像的 SRC 属性?
<a href="#">
<img src="./layout/images/search.png" />
</a>
$('img[src="./layout/images/search.png"]').click(function () {
$(this).attr('src',"./layout/images/search_select.png")
});
How can I swap an image's SRC attribute, on click of that image?
<a href="#">
<img src="./layout/images/search.png" />
</a>
$('img[src="./layout/images/search.png"]').click(function () {
$(this).attr('src',"./layout/images/search_select.png")
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试以下解决方案:
正如 David Hedlund 指出的那样,由于
img
选择器,这将更改页面上的所有图像。您可以按照 sam152 的建议使用类来定位图像,或者您可以使用它的src
属性来定位图像 -请参阅我的演示 - JSFiddle
You can try this solution:
As David Hedlund points out this will change all images on a page due to the
img
selector. You could target an image using a class as sam152 suggests or, you could target an image using it'ssrc
attribute -See my demo - JSFiddle
如果您有一个名为“swap”的类的图像,则可以使用以下代码片段。它使用 click 事件,然后使用 attr 来更改图像 src 属性。
If you have an image with a class called 'swap', you can use the following snippet. It uses the click event, and then attr, to change the images src attribute.