简体   繁体   English

添加/删除类时,jQuery单击函数表现奇怪

[英]jQuery click function acting strange when adding/removing classes

I have two list items that, when clicked, should change classes from '.off' to '.on'. 我有两个列表项,单击时,应该将类从“.off”更改为“.on”。 Only one element should be '.on' at a time so when one is already turned on and the other is clicked both elements should change classes from '.off' to '.on' and vice versa. 一次只有一个元素应该是'.on',所以当一个元素已经打开而另一个元素被点击时,两个元素都应该将类从'.off'改为'.on',反之亦然。 If a list item with a class of '.on' is clicked it should change classes to '.off' 如果单击类别为“.on”的列表项,则应将类更改为“.off”

The problem I am having is when a list item with class '.on' is clicked it still runs the click function as if it had a class of '.off' 我遇到的问题是当单击类“.on”的列表项时,它仍然运行click函数,就像它有一个'.off'类一样

My html: 我的HTML:

<ul>
    <li><a href="about" class="off">ABOUT</a></li>
    <li><a href="upload" class="off">SUBMIT</a></li>
</ul>

My javascript (running on jQuery 1.7.1) 我的javascript(在jQuery 1.7.1上运行)

$('.off').click(function(event) {
    event.preventDefault();
    $(".on").addClass("off").removeClass("on");
    $(this).addClass("on").removeClass("off");
});
$('.on').click(function(event) {
    event.preventDefault();
    $(this).addClass("off").removeClass("on");
});

Does anyone know what is going on here? 有谁知道这里发生了什么? Is there something wrong in my code or have I encountered some sort of bug here? 我的代码中有什么问题或我在这里遇到过某种错误吗?

http://jsfiddle.net/ZC3CW/6/ http://jsfiddle.net/ZC3CW/6/

The selectors you're using to bind the event using click() are used to select the elements to add the event handler to. 您用于使用click()绑定事件的选择器用于选择要添加事件处理程序的元素。 The selector is not considered when the handler is run. 运行处理程序时不考虑选择器。

You should be looking for something more like this: 你应该寻找更像这样的东西:

$('li').click(function(event) {
    event.preventDefault();

    if ($(this).hasClass('off')) {
        $(".on").addClass("off").removeClass("on");
        $(this).addClass("on").removeClass("off");
    } else { // $(this).hasClass('on');
        $(this).addClass("off").removeClass("on");
    }
});

You might want to make the li selector more explicit by adding a class/id to the ul or li 's. 您可能希望通过向ulli添加类/ id来使li选择器更明确。

To confuse things further, you could also do this (if you're using jQuery > 1.7); 为了进一步混淆,你也可以这样做(如果你使用的是jQuery> 1.7);

$(document).on('click', '.off', function(event) {
    event.preventDefault();
    $(".on").addClass("off").removeClass("on");
    $(this).addClass("on").removeClass("off");
});
$(document).on('click', '.on', function(event) {
    event.preventDefault();
    $(this).addClass("off").removeClass("on");
});

This is because the .on() function works by attaching the event handler to the selected elements ( document ), and will only execute the handler (the function) on the event specified ( click ) if the element that the event originated from matches the selector .off at the time the event fired, not at binding time . 这是因为.on()函数通过将事件处理程序附加到所选元素( document )来工作,并且仅在指定事件( click )上执行处理程序(函数),如果事件源自的元素与事件发生时的选择器.off ,而不是绑定时间

I would suggest adding a click handle to a different selector, this should work for you... 我建议将一个点击手柄添加到另一个选择器,这应该对你有用...

$("ul li a").click(function(e){
   e.preventDefault();

   if($(this).hasClass("off")){
      $("ul li a").addClass("off").removeClass("on");
      $(this).addClass("on").removeClass("off");
   }
   else{
      $(this).addClass("off").removeClass("on");
   }
});

The problem is that jQuery handlers get attached at page load and remain the same regardless of changing their classes. 问题是jQuery处理程序在页面加载时附加,并且无论是否更改类,都保持不变。 Use live('click', handler) on('click', handler) instead of click(). 使用 live('click',handler) on('click',handler)而不是click()。

Edit: just noticed that .live() is deprecated in jQuery 1.7. 编辑:刚才注意到jQuery 1.7中不推荐使用.live()。

The problem as I see it is that your "on" class is not in play at the time of the click event, so your $('.on').click method is never being called. 我认为这个问题是你的“on”类在click事件发生时没有播放,因此你的$('.on').click方法永远不会被调用。

Try re-assigning your events after changing classes (example follows) : 尝试在更改类后重新分配事件(示例如下):

var assignClicks = function () {
    $('.off').click(function(event) {
        event.preventDefault();
        $(".on").addClass("off").removeClass("on");
        $(this).addClass("on").removeClass("off");
        assignClicks();
    });
    $('.on').click(function(event) {
        event.preventDefault();
        $(this).addClass("off").removeClass("on");
        assignClicks();
    });
};
assignClicks();

Hope this helps, 希望这可以帮助,

Pete 皮特

The click is bound to the element not the class. 单击绑定到元素而不是类。 Maybe you can attach the events to the 也许你可以将事件附加到

  • elements and detect/toggle the elements classes: 元素和检测/切换元素类:

     $('li').click(function(event) { event.preventDefault(); if( $(this).hasClass('on') ) { $(this).removeClass('on'); } else { $(this).addClass('on'); $(this).siblings().removeClass('on'); } }); 
  • 声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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