简体   繁体   English

jQuery-.removeClass不起作用

[英]jQuery - .removeClass doesn't work

I have a button <button id="subs1">test</button> and a div 我有一个按钮<button id="subs1">test</button>和一个div

<div class="subsystem subhide">
  <h3 style="text-align: center; font-size: 30px; font-weight: normal;">Electronics</h3>
  <p>Lorem</p>
</div>

In the css file the class .subhide is defined as 在css文件中,类.subhide定义为

.subhide {
 display: none;
}

and the jQuery code that I'm trying to get to work is 而我想开始工作的jQuery代码是

jQuery('#subs1').on('click', function(){
jQuery('.subsystem').removeClass('subhide');
});

When clicking the button nothing happens, why is that? 当单击按钮时什么也没发生,那是为什么?

Thanks in advance! 提前致谢!

The problem is that jQuery('#subs1') finds elements with that id and binds to them. 问题是jQuery('#subs1')找到具有该ID的元素并将其绑定。 If elements don't have the id at the time the binding is made, the events will not be handled. 如果在进行绑定时元素没有id,则将不处理事件。

Attach on DOM ready 附上DOM准备就绪

To solve this issue, wrap the code with on document ready , this way your code will execute only when your dom (document) is ready. 要解决此问题,请将代码包装on document ready ,这样,仅当dom(文档)准备好时,您的代码才会执行​​。

A page can't be manipulated safely until the document is "ready." 在文档“就绪”之前,无法安全地操纵页面。 jQuery detects this state of readiness for you. jQuery为您检测到这种就绪状态。 Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute. $( document ).ready()内包含的代码仅在页面Document Object Model(DOM)准备好执行JavaScript代码后才能运行。

A better approach 更好的方法

On the other hand, if your button is dynamically added, it will not be there when the event listener wants to be binded, so the code will practically do nothing. 另一方面,如果您的按钮是动态添加的,则当事件监听器要绑定时,按钮将不存在,因此代码实际上将不执行任何操作。 To solve this issue, change your code to the following: 要解决此问题,请将您的代码更改为以下内容:

jQuery(document.body).on('click', '#subs1', function() {
    jQuery('.subsystem').removeClass('subhide');
});

This event will be attached to the document.body which is always there in the DOM tree. 此事件将附加到DOM树中始终存在的document.body Now when the body is clicked, the event will propagate to #subs1 regardless of the time the button was attached to the DOM. 现在,单击主体时,无论按钮连接到DOM的时间如何,事件都将传播到#subs1

Working Snippet 工作片段

Here is a working example: 这是一个工作示例:

 $( document ).ready(function() { jQuery(document.body).on('click', '#subs1', function() { jQuery('.subsystem').removeClass('subhide'); }); jQuery(document.body).on('click', '#subs2', function() { jQuery('.subsystem').addClass('subhide'); }); }); 
 .subhide { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="subs1">Unhide</button> <button id="subs2">hide</button> <div class="subsystem subhide"> <h3 style="text-align: center; font-size: 30px; font-weight: normal;">Electronics</h3> <p>Lorem</p> </div> 

 jQuery('#subs1').on('click', function(){ jQuery('.subsystem').removeClass('subhide'); }); jQuery('#toggle').on('click', function(){ jQuery('.subsystem').toggle('subhide'); }); 
 .subhide { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="subsystem subhide"> <h3 style="text-align: center; font-size: 30px; font-weight: normal;">Electronics</h3> <p>Lorem</p> </div> <button id="subs1">Button</button> <button id="toggle">Toggle Button</button> 

You need to define button outside of class 您需要在课外定义按钮

Hope this help! 希望有帮助!

Try it: 试试吧:

    $( document ).ready(function() {
      $('#subs1').on('click', function(){
          $('body').find('.subhide').removeClass('subhide');
      });
   });

and if you use chrome, clear your browser cache. 如果您使用的是chrome,请清除浏览器缓存。

In the stylesheet, change it to: 在样式表中,将其更改为:

.subsystem.subhide {
display: none;
}

.subsystem {
display: anything;
} }

It should work 它应该工作

I'm not really sure why, but this snippet works: 我不确定为什么,但是此片段有效:

jQuery( document ).ready(function() {
 jQuery('#subs1')
  .click(function(){
  jQuery('body').find('.subsystem').addClass('subhide')})
});

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

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