2010-11-01 94 views
0

如何选择某个<div>以外的所有子元素,但活动的元素除外?例如:jquery选择器问题

<div> 
    <a id="1" class="item" href="#">Item 1 </a> 
    <a id="2" class="item" href="#">Item 2 </a> 
    <a id="3" class="item" href="#">Item 3 </a> 
</div> 

<script> 
$(function() { 
    $(".item").mouseover(function() { 


     // HOW TO hide all the items with class item except this one 


    }); 
}); 

回答

1

您可以使用.not()排除this(当前元素),就像这样:

$(function() { 
    $(".item").mouseover(function() { 
     $(".item").not(this).hide(); 
    }); 
}); 

或者,如果他们总是兄弟姐妹使用.siblings(),就像这样:

$(function() { 
    $(".item").mouseover(function() { 
     $(".item").siblings().hide(); 
    }); 
}); 
+0

好主人,我打败@Nick Craver(几乎相同的答案)整整17秒* O_O虽然,虽然,我的变化没有奏效,所以这确实需要一些快乐...... =/ – 2010-11-01 13:41:39

+0

@David - 你的回答会抛出一些非常奇怪的错误,'this'不是一个字符串,它是一个DOM元素。 – 2010-11-01 13:42:23

+0

确实,请参阅上面的我的(编辑)评论。诅咒 – 2010-11-01 13:44:35

1
$('.item').not($(this).show()).hide(); 
+0

哈哈我做了同样的答案=) 删除它虽然 – Breezer 2010-11-01 13:43:10

+0

没有必要包装'this'或'.show()'它...如果它不*已经*可见,我们不能把它挖出来。 – 2010-11-01 13:45:20

0

不同的想法...隐藏所有,然后告诉我:

$(function() { 
    $(".item").mouseover(function() { 

     // Hide all: 
     $('.item').hide(); 

     // Show "me": 
     $(this).show(); 


    }); 
}); 
0

可以使用$(this)选择过度除了在鼠标的项目。