简体   繁体   English

jQuery选择器-

[英]JQuery selector -

I'm going mad trying to figure out why my code won't work. 我很生气,试图弄清楚为什么我的代码无法正常工作。

I am setting an li element with an active class, which works.. 我正在使用活动类设置li元素,该方法有效。

i then want to select that li element which has class 'active' - and give it a click function that slides another div into view, which is also identified by the class 'active' 然后,我要选择具有“活动”类的li元素-并为其提供一个单击功能,将另一个div滑入视图,这也由“活动”类标识

here is my code: 这是我的代码:

$(".contentPanel").click(function() {
  $(this).siblings().animate({height: minHeight,},500).removeClass("active");
  $(this).animate({height: maxHeight,},500).addClass("active");
  $('li').removeClass("active");
  $(this).children('ul').children('li').addClass("active");
  $('.infoPanelSlider').removeClass("active");
  $(this).children('.infoPanelWindow').children('.infoPanelSlider').addClass("active");
});

The above works, the li elements inside the active 'contentPanelSlider' are given the class active. 在上面的工作中,活动“ contentPanelSlider”内的li元素被赋予了活动类。

The next bit is where i"m stuck: 下一点是我被卡住的地方:

I want to write this: 我想这样写:

$('li.active').click(function().... do something $('li.active')。click(function()....做点什么

but the 'active part doesnt seem to work - it works if i just target the 'li', without the active class selector... 但是'活动部分似乎不起作用-如果我只是针对'li',而没有活动的类选择器,它就可以工作...

I'm wondering if this has something to do with the fact that the active class was added by JQuery on the fly, and not hard coded in the stylesheet - but this is a complete guess.. 我想知道这是否与以下事实有关:活动类是由JQuery动态添加的,而不是在样式表中进行硬编码的-但这是一个完整的猜测。

Please help if you can, I dont mind getting flamed... 如果可以的话请帮忙,我不介意被炒鱿鱼...

You could attach the handler to all li and check whether they have class active : 您可以将处理程序附加到所有li并检查它们是否具有active类:

$('li').click(function() {
    if($(this).is('.active')) {
        // do more
    }
});

Even better (if you have a lot of li s) is to delegate the event. 最好(如果您有很多lidelegate事件。 This will only add one event handler (whereas the previous solution will add an event handler to every li element and thus consuming more memory). 这只会添加一个事件处理程序(而先前的解决方案将为每个 li元素添加一个事件处理程序,从而消耗更多的内存)。 You just need to have a common ancestor: 您只需要有一个共同的祖先:

ancestor.delegate('li.active', 'click', function() {
      // do more
});

$('li.active') will only work if the li s have this class at the moment when you assign the handler not when the handler is executed. $('li.active')仅在li 在分配处理程序时具有此类而不在执行处理程序时具有此类时才起作用。 Thus: 从而:

$(".contentPanel").click(function(){});
$("li.active").click(function(){});

won't work if there are no li elements with this class yet, but this 如果此类中没有li元素,将无法正常工作,但这

$(".contentPanel").click(function() {
    // add class to li
    $('li.active').click(function(){});
});

would work. 会工作。 But it would also add the event handler every time, so assigning once and testing is better. 但是它还会每次都添加事件处理程序,因此分配一次并进行测试会更好。

Yes, your suspicions are correct. 是的,您的怀疑是正确的。 jQuery selectors resolve at the time of execution. jQuery选择器在执行时解析。 If you are looking for li.active during the starting website, nothing matches. 如果您在启动网站期间正在寻找li.active ,则没有匹配项。 jQuery offers live() and delegate() which can get around this problem by checking every click event at a higher level. jQuery提供了live()delegate() ,可以通过在更高级别检查每个click事件来解决此问题。

// will match any click event on any li with the class active when it is clicked:
$('li.active').live('click', function() {
   alert('Active Clicked');
});

Yes. 是。 Its happening because the class was added on the fly. 之所以会这样,是因为该类是动态添加的。 Use jquery's live function to bind the click event to the li 使用jquery的live函数将click事件绑定到li

$('li.active').live('click',function(){ //Do something. }); $('li.active')。live('click',function(){//做某事。});

If live doesn't work you can try bind: 如果live不起作用,则可以尝试绑定:

$('li.active').bind('click',function(){
//do something 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM