2013-01-17 14 views
3

我有一个使用:not()选择器/排除器的问题。我正在做的是增加一个'.Activated'类来开放手风琴小组(class'.panel')。然后,当使用'.panel'类悬停所有元素时,我想运行一个函数。但是,有时候会有两个类'class ='panel Activated''的元素,我不想在其上运行该功能。jQuery:not()selector addClass()之后的元素不工作(实时/实时)

而且,这里是我的一些相关的代码:不()选择不捡:

function onOpenPanel(obj){ 
     var slideR = obj.index + 1; 
     $('.panel:nth-child(' + slideR + ')').addClass('Activated'); 
    } 

$('.panel:not(.Activated)').hover(function(){ 
     $(this).css('background-position','0px top'); 
    },function(){ 
     $(this).css('background-position','-41px top'); 
    }); 

你会认为在打开的手风琴面板背景跳跃41px因为我在网站上看到该面板还具有'.Activated'类,因此不应受到.hover函数的影响。

HTML:

<div class="accordion"> 

    <!-- First slide --> 

    <div> 
     <img src="img/img-1.jpg" width="10" /> 

     <div class="caption"> 
     <p class="mask-1"></p> 
      <p class="title sl1"> 
       Professional Products & Solutions 
      </p> 
      <p class="body one">As a global leader across many sectors, Sony Professional combine world class knowledge with groundbreaking technology – inspiring businesses to amaze their customers.<br /> 
      </p> 

     </div> 

    </div> 

    <!-- First slide --> 

    <!-- Second slide --> 

    <div> 
     <img src="img/img-2-1.jpg" /> 

     <div class="caption"> 
     <p class="mask-2"></p> 
      <p class="title sl2"> 
       4K Digital Cinema 
      </p> 
      <p class="body two">Taking digital cinema to new levels of immersive engagement, the superior resolution of Sony 4K creates the ultimate in crowd-pleasing experiences – partner with us for business growth. 
      </p> 

      <a href="http://stg.sony.co.uk/pro/hub/digital-cinema" class="slide-link">Read more<img src="img/cta-block.jpg" class="cta-block"/></a> 
     </div> 

    </div> 

    <!-- Second slide --> 

    <!-- Third slide --> 

    <div class="sonySlideIn"> 
     <img src="img/img-3.jpg" /> 

     <div class="caption"> 
     <p class="mask-3"></p> 
      <p class="title sl3"> 
       Broadcast & Pro AV 
      </p> 
      <p class="body three">Shoot, edit, broadcast, archive. Whatever your priority – from image quality to production efficiency – Sony enables you realise your vision without compromises. 
      </p> 
      <img src="img/prod-3.jpg" width="400" height="98" id="prod-3"/> 
      <a href="http://stg.sony.co.uk/pro/hub/broadcast-professional-audio-video" class="slide-link">Read more<img src="img/cta-block.jpg" class="cta-block"/></a> 
     </div> 

    </div> 

    <!-- Third slide --> 

    <!-- Fourth slide --> 

    <div class="sonySlideIn"> 
     <img src="img/img-4.jpg" /> 

     <div class="caption"> 
     <p class="mask-4"></p> 
      <p class="title sl4"> 
       Industrial Cameras 
      </p> 
      <p class="body four">From machine vision to visual communications and OEM conference solutions, see how the application of Sony imaging expertise delivers market-leading solutions for industrial environments. 
      </p> 
      <img src="img/prod-4.jpg" id="prod-4"/> 
      <a href="http://stg.sony.co.uk/pro/hub/industrial-machine-vision-cameras" class="slide-link">Read more<img src="img/cta-block.jpg" class="cta-block"/></a> 
     </div> 

    </div> 

    <!-- Fourth slide --> 

    <!-- Fifth slide --> 

    <div class="sonySlideIn"> 
     <img src="img/img-5.jpg" /> 

     <div class="caption"> 
     <p class="mask-5"></p> 
      <p class="title sl5"> 
       Medical 
      </p> 
      <p class="body five">Driving the future of medical imaging by redefining clarity across monitors, cameras, radiology imagers, printers and recorders, Sony Medical creates pioneering solutions – including 3D – that enable clearer diagnoses and more efficient workflows. 
      </p> 
      <img src="img/prod-5.jpg" id="prod-5"/> 
      <a href="http://stg.sony.co.uk/pro/hub/medical" class="slide-link">Read more<img src="img/cta-block.jpg" class="cta-block"/></a> 
     </div> 

    </div> 

    <!-- Fifth slide --> 

    <!-- Sixth slide --> 

    <div class="sonySlideIn"> 
     <img src="img/img-6.jpg" /> 

     <div class="caption"> 
     <p class="mask-6"></p> 
      <p class="title sl6"> 
       Projectors, Displays & Digital Signage 
      </p> 
      <p class="body six">Designed to attract attention, engage interest and add the wow factor to professional environments, Sony bring superior image quality to businesses, organisations, education establishments and venues of every size. 
      </p> 
      <img src="img/prod-6.jpg" id="prod-6"/> 
      <a href="http://stg.sony.co.uk/pro/hub/displays-projectors-digital-signage" class="slide-link">Read more<img src="img/cta-block.jpg" class="cta-block"/></a> 
     </div> 

    </div> 

    <!-- Sixth slide --> 

    <!-- Seventh slide --> 

    <div class="sonySlideIn"> 
     <img src="img/img-7.jpg" /> 

     <div class="caption"> 
     <p class="mask-7"></p> 
      <p class="title sl7"> 
       Sports & Stadiums 
      </p> 
      <p class="body seven">Maximising customer engagement and satisfaction, our range of leading AV solutions for large arenas drives footfall, spend, loyalty and advocacy – see the advantages of partnering with Sony. 
      </p> 
      <a href="http://stg.sony.co.uk/pro/hub/solutions-stadiums-arenas" class="slide-link">Read more<img src="img/cta-block.jpg" class="cta-block"/></a> 
     </div> 

    </div> 

    <!-- Seventh slide --> 

    <!-- Eigth slide --> 

    <div class="sonySlideIn"> 
     <img src="img/img-8.jpg" /> 

     <div class="caption"> 
     <p class="mask-8"></p> 
      <p class="title sl8"> 
       Video Security 
      </p> 
      <p class="body eight">See the most technically advanced, network based, end-to-end video security solutions – from hybrid and Full HD IP cameras to recorders and third-party compatible software. 
      </p> 
      <img src="img/prod-8.jpg" id="prod-8"/> 
      <a href="http://stg.sony.co.uk/pro/hub/video-security" class="slide-link">Read more<img src="img/cta-block.jpg" class="cta-block"/></a> 
     </div> 

    </div> 

    <!-- Eigth slide --> 

</div> 
+0

请添加HTML。 – Jrod

+0

@JamesHill一些我的小脑子发现了代码和问题的区别,但没有发现第一个代码!谢谢 – ManseUK

+0

你知道,'$('。panel:not(.Activated)')。hover'只会影响那些在特定代码运行时(这只是一次)未激活的元素,是正确的吗?在运行该代码行之后,添加.Activated元素将不会更改悬停事件绑定到的元素。已添加HTML –

回答

5

jQuery选择返回时执行代码匹配的元素,使发生后,他们将不会反映后更改DOM。

当代码执行时(大概在DOM准备就绪时),您的代码会将hover事件处理程序绑定到没有Activated类的元素,但添加该类将不会删除这些事件处理程序。

如果您希望它们动态反应,请查看event delegation

+0

谢谢,我要去看看这个网页。 .on()是否替换为.live()?我想我已经使用.live()来处理与过去非常相似的事情。 –

+0

@danielblythe在。1.7中添加了'.on()'函数来处理**所有**事件绑定,包括事件委托(以前由'.live()'和'.delegate()'处理。 –

+0

OK谢谢,所以将.on()与悬停作为事件一起工作吗?例如,我正在尝试这种方式,但它不起作用? '$('。panel:not(.Activated)')。on(“hover”功能(){ \t \t \t $(本)的.css( 'background-position属性', '0像素顶级'); \t \t});' –