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

jQuery: elements with class but not $(this)

本文关键字:this 元素 具有 class jQuery      更新时间:2023-09-26

我有一个图片库,里面有图片,在鼠标悬停在一张图片上时,我想对所有其他图片做些什么(所以所有图片都有.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!

使用.not

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