jQuery:具有类但不是$(this)的元素

时间:2012-08-30 13:08:09

标签: javascript jquery jquery-selectors

我有一个带图片的图库和鼠标悬停在1张图片上我想用所有其他图片做(所以所有图片都有.picture,除了一个悬停($(this) )。

我尝试将类应用到类.picture的所有图片,然后在图片悬停时删除这个添加的类,然后使用添加的类(其中包含正确的图片),但它不是工作顺利......

有更干净,更好的方法吗?

示例:

pic 1 -> do sth with this
pic 2 -> do sth with this
pic 3 -> gets hovered, don't do anything with this, exclude it from the selection!

2 个答案:

答案 0 :(得分:6)

使用.not

$('.picture').not(this);

答案 1 :(得分:3)

var img = $('.picture');
img.on('hover', function() {
    img.not(this).each(function() {
       /* do Something with $(this); */
    })
});