简体   繁体   English

JQuery:使用:not(.active) 选择器,并将 Active class 添加到所选项目

[英]JQuery: Using :not(.active) selector, and adding an Active class, to the item selected

I'm new to Javascript and am having a bit of an issue with using a NOT selector, and adding a class during the function, hopefully this will make sense to someone.我是 Javascript 的新手,在使用 NOT 选择器和在 function 期间添加 class 时遇到了一些问题,希望这对某人有意义。

I am creating a small gallery, and my goal is to have clickable navigation, however the active image will redirect to another page when clicked.我正在创建一个小画廊,我的目标是拥有可点击的导航,但是点击时活动图像将重定向到另一个页面。

Code is as follows:代码如下:

    $("ul#mainGallery li:not(.active) a").click(function(){
      var thisListClass = $(this).parent().attr('class');
         var activeListId = $(this).parent().attr('id');
         var newMarginLeft = (activeListId-3) * -200;
         var animateAction = {};
          animateAction['margin-left'] = newMarginLeft + 'px';
        $("ul#mainGallery").animate(animateAction, 1000);
        $('li.active img').animate({width:'100px', height:'100px'},1000)
        $(this + 'img').animate({width:'300px', height:'300px'},1000)
        $(li.active).removeClass('active');
        $(this).parent().addClass('active');
        return false;

I know there is likely a much better way to do this, but I can't get my head around it.我知道可能有更好的方法来做到这一点,但我无法理解它。

Edit: I should probably say what the problem is...编辑:我可能应该说问题是什么......

When an active image is clicked, it follows the hyperlink all is well.单击活动图像时,它会跟随超链接一切正常。

When a non active image is clicked, it begins the animation, then (i assume) when the 'active' class is added, instead of returning false, it returns true and follows the hyperlink.单击非活动图像时,它会开始 animation,然后(我假设)当添加“活动”class 时,它不会返回 false,而是返回 true 并遵循超链接。

To stop the default behaviour use the preventDefault() function要停止默认行为,请使用 preventDefault() function

$("ul#mainGallery li:not(.active) a").click(function(e){
   e.preventDefault(); // will stop the default behaviour
}

Read more on Jquery docs阅读有关Jquery 文档的更多信息

You are binding the click event to $("ul#mainGallery li:not(.active) a") whenever that code is run (presumably on document load).无论何时运行该代码(可能是在文档加载时),您都将单击事件绑定到$("ul#mainGallery li:not(.active) a") ) 。 The items which are not active at that point will have that item bound, and changing the class afterwards on other items won't bind this event to them.此时未激活的项目将绑定该项目,并且之后在其他项目上更改 class 不会将此事件绑定到它们。 You will need to either change how you bind it or check inside the function whether the item has that class.您将需要更改绑定方式或检查 function 中的项目是否具有该 class。

Something like this:像这样的东西:

$("ul#mainGallery li a").click(function(){
if(!$(this).parent().hasClass('active')){


      var thisListClass = $(this).parent().attr('class');
         var activeListId = $(this).parent().attr('id');
         var newMarginLeft = (activeListId-3) * -200;
         var animateAction = {};
          animateAction['margin-left'] = newMarginLeft + 'px';
        $("ul#mainGallery").animate(animateAction, 1000);
        $('li.active img').animate({width:'100px', height:'100px'},1000)
        $(this + 'img').animate({width:'300px', height:'300px'},1000)
        $('li.active').removeClass('active');
        $(this).parent().addClass('active');
        return false;
}

EDIT, or if you prefer to continue using the same selector with the :not and everything, then switch your click function to .live()编辑,或者如果您希望继续使用与:not和所有内容相同的选择器,则将click function 切换为.live()

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

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