admin管理员组文章数量:1387292
I have a button <button id="subs1">test</button>
and a 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
.subhide {
display: none;
}
and the jQuery code that I'm trying to get to work is
jQuery('#subs1').on('click', function(){
jQuery('.subsystem').removeClass('subhide');
});
When clicking the button nothing happens, why is that?
Thanks in advance!
I have a button <button id="subs1">test</button>
and a 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
.subhide {
display: none;
}
and the jQuery code that I'm trying to get to work is
jQuery('#subs1').on('click', function(){
jQuery('.subsystem').removeClass('subhide');
});
When clicking the button nothing happens, why is that?
Thanks in advance!
Share Improve this question edited Sep 2, 2017 at 14:45 Dekel 62.7k12 gold badges108 silver badges130 bronze badges asked Sep 2, 2017 at 14:42 SkydiverSkydiver 1211 silver badge3 bronze badges 9- 1 can you check for errors also create a demo? – guradio Commented Sep 2, 2017 at 14:45
- 1 What is the role of subsystem? Add subsystem CSS please – haMzox Commented Sep 2, 2017 at 14:45
- 1 Did you include the jquery library? – Dekel Commented Sep 2, 2017 at 14:46
- 1 working jsfiddle/78k8dkq5 – Madhawa Priyashantha Commented Sep 2, 2017 at 14:46
- 1 Whats your jquery version? .on was introduced since 1.7 – Alex Shchur Commented Sep 2, 2017 at 15:25
5 Answers
Reset to default 2Try it:
$( document ).ready(function() {
$('#subs1').on('click', function(){
$('body').find('.subhide').removeClass('subhide');
});
});
and if you use chrome, clear your browser cache.
The problem is that jQuery('#subs1')
finds elements with that id and binds to them. If elements don't have the id at the time the binding is made, the events will not be handled.
Attach on DOM ready
To solve this issue, wrap the code with on document ready
, this way your code will execute only when your dom (document) is ready.
A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside
$( document ).ready()
will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.
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. Now when the body is clicked, the event will propagate to #subs1
regardless of the time the button was attached to the DOM.
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./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>
In the stylesheet, change it to:
.subsystem.subhide {
display: none;
}
.subsystem {
display: anything;
}
It should work
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./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!
I'm not really sure why, but this snippet works:
jQuery( document ).ready(function() {
jQuery('#subs1')
.click(function(){
jQuery('body').find('.subsystem').addClass('subhide')})
});
本文标签: javascriptjQueryremoveClass doesn39t workStack Overflow
版权声明:本文标题:javascript - jQuery - .removeClass doesn't work - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744569106a2613233.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论