Jquery .not()更改图像src - data-other-src

时间:2017-01-24 00:20:42

标签: jquery html

尝试使用Jquery将img src更改为data-other-src。我们不能用.not()吗?

实现

Jquery的:

 $('.listItem').not(this).attr({src:$(this).attr("data-other-src")});

HTML(有很多链接):

 <a href="#" class="listItem"><img src="../img/updown.png" class="previewImg" data-other-src="../img/here.png"/></a>

2 个答案:

答案 0 :(得分:1)

srcdata-other-src属性位于img,而不是.listItem,因此您需要使用.child()来访问img

$('.listItem').not(this).children("img").attr('src', $(this).children("img").data("other-src"));

答案 1 :(得分:0)

尝试以下操作,在not()

中发送正确的jQuery对象
$('.listItem').not($(this)).attr( 'src' ,$(this).attr("data-other-src"));