2015-12-03 57 views
0

我想在jQuery中使not运算符工作。它根本不工作。jQuery - 不是运营商没有按预期工作

悬停应在容器ca工作,但它不应该其子容器上工作noHover

FIDDLE HERE

我的HTML结构:

<div class="ca" style="height:40%"> 
    <div class="imageContainer"> 
     <span class="hoverEffect">Some Image</span> 
     <span class="noHover">NH</span> 
    </div> 
    <div class="showContainer"> 
     <span>New Image</span> 
    </div> 
</div> 

的jQuery:

$('.ca').not('.noHover').hover(function() { 
    $('.imageContainer', this).hide(); 
    $('.showContainer', this).show(); 

}, function() { 
    $('.imageContainer', this).show(); 
    $('.showContainer', this).hide(); 
}); 
+3

“不工作” 是没用的。你需要解释你想要的代码做什么,以及它在做什么,你不想做什么(或者做什么不是你想做的事)。你想瞄准'.hoverEffect'吗? – Amadan

+0

你想要这个 - http://jsfiddle.net/qsLg1ry5/5/ –

+0

@Amadan更新了问题 – user2281858

回答

1

使用不同的选择ORS和背景:

$('.ca span:not(.noHover)').hover(function() { 
    $('.imageContainer', $(this).closest('.ca')).hide(); 
    $('.showContainer', $(this).closest('.ca')).show(); 

}, function() { 
    $('.imageContainer', $(this).closest('.ca')).show(); 
    $('.showContainer', $(this).closest('.ca')).hide(); 
}); 

考虑到问题的更新,你可能要为元素的处理程序附加到一个不同的选择:

'.ca, .ca *:not(.noHover)' 
0

jQuery的not从匹配的元素集合中删除元素。 .noHover不是匹配元素$('.ca')的集合。你可以像下面这样做。

$('.ca').hover(function (e) { 
    if(e.target.className=='hoverEffect'){ 
     $('.imageContainer', this).hide(); 
     $('.showContainer', this).show(); 
    }  
}, function() { 
     $('.imageContainer', this).show(); 
     $('.showContainer', this).hide(); 
}); 

JS小提琴:http://jsfiddle.net/qsLg1ry5/6/