简体   繁体   English

为什么Jquery on()函数不适用于类?

[英]Why Jquery on() function does not work with class?

I'm trying to add and remove the class on click event. 我正在尝试添加和删除click事件上的类。 I'm adding and removing the three classes onclick event of .the-old , its working properly. 我要添加和删除.the-old的三个类onclick事件,它可以正常工作。

Onclick Event (.the-old) Onclick事件(.the-old)

  1. add class on button tag (.the-new) 在按钮标签上添加类(.the-new)
  2. Add and remove class on ids #navbar-hamburger-01 and #navbar-closed 在ID #navbar-hamburger-01#navbar-closed上添加和删除类

JS JS

$(function() {

  $('.the-old').on('click',  function() {
        //alert('..');
     $('#navButtonToggle').addClass('the-new');
     $('#navButtonToggle').removeClass('the-old');
       $('#navbar-hamburger-01').addClass('hidden');
       $('#navbar-closed').removeClass('hidden');    
    });

    $('.the-new').on('click',  function() {
        alert('..');

       $('#navbar-hamburger-01').removeClass('hidden');
       $('#navbar-closed').addClass('hidden');    
    });

});

HTML 的HTML

<button id="navButtonToggle" type="button" data-target="#navbarCollapse" data-toggle="collapse" class="navbar-toggle repnsive-navbar-toggle the-old">
    <div id="navbar-hamburger-01">
    <span class="sr-only">Toggle navigation</span>

    <span class="icon-bar"></span>

    <span class="icon-bar"></span>

    <span class="icon-bar"></span>
    </div>
    <div id="navbar-closed" class="hidden">
    <span class="glyphicon glyphicon-remove"></span>
    </div>
</button>

But this function does not working $('.the-new').on('click', function() {}) , even i just try to alert , but it does not working. 但是,此函数不起作用$('.the-new').on('click', function() {}) ,即使我只是试图发出alert ,但它也不起作用。 However class .the-new is properly adding onclick event of the-old . 但是.the-new类正确添加了the-old onclick事件。 Can any guide me where i'm wrong. 任何人都可以指导我哪里做错了。

The reason for this already explained in previous answer. 前面的答案中已经解释了这样做的原因。 To make it work you can integrate either of these changes 要使其正常工作,您可以集成以下任一更改

    <button id="navButtonToggle" type="button" data-target="#navbarCollapse" data-toggle="collapse" class="navbar-toggle repnsive-navbar-toggle the-old">
        <div id="navbar-hamburger-01">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        </div>
        <div id="navbar-closed" class="hidden">
        <span class="glyphicon glyphicon-remove"></span>
        </div>
    </button>
<script>
  $(function() {
          $('.the-old').on('click',  function() {
                //alert('..');
             $('#navButtonToggle').addClass('the-new');
             $('#navButtonToggle').removeClass('the-old');
               $('#navbar-hamburger-01').addClass('hidden');
               $('#navbar-closed').removeClass('hidden');    
            });
           $('.the-new').on('click',  function() {
                alert('..');
            $('#navbar-hamburger-01').removeClass('hidden');
               $('#navbar-closed').addClass('hidden');    
            });
       });
    </script>

OR 要么

delegate the event without making any changed to HTML 委托事件,而无需对HTML进行任何更改

$(function() {

  $('body').on('click','.the-old', function() {
        //alert('..');
     $('#navButtonToggle').addClass('the-new');
     $('#navButtonToggle').removeClass('the-old');
       $('#navbar-hamburger-01').addClass('hidden');
       $('#navbar-closed').removeClass('hidden');    
    });

    $('body').on('click','.the-new'function() {
        alert('..');

       $('#navbar-hamburger-01').removeClass('hidden');
       $('#navbar-closed').addClass('hidden');    
    });

});

When you register the event handlers for click, the class the-new does not exist so when you actually register it nothing happens. 当您注册用于click的事件处理程序时, 类不存在,因此当您实际注册它时,什么也不会发生。

You need to register it inside the handler of add-new . 您需要在add-new的处理程序中注册它。 This is however risky because you need to "de-register" it once the-new is removed or else you will: 1) Have duplicate handlers that grow on every click. 但是,这样做存在风险,因为一旦删除了新的内容,您需要“注销”它,否则您将:1)每次单击都有重复的处理程序。 2) Handle the-XXX event even if its not logically what you meant. 2)处理-XXX事件,即使这在逻辑上不是您的意思。

I would suggest creating a common base css class for the element and working on it, checking what is the state, then adding the right class accordingly. 我建议为该元素创建一个通用的基本CSS类并对其进行处理,检查状态是什么,然后相应地添加正确的类。 You already do it when you register the 1st handler via element ID. 通过元素ID注册第一个处理程序时,您已经完成了操作。

Also note that in the callback you don't need to do a DOM query for the element, you can get it from the $event parameter supplied when the handler gets invoked. 还要注意,在回调中,您不需要为元素进行DOM查询,您可以从调用处理程序时提供的$ event参数中获取它。

See the jQuery docs for that. 请参阅jQuery文档。

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

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