admin管理员组文章数量:1426066
I want to hide some elements on the page that use the class 'd-md-block'. Because of the important on the class definition i can not use jquery hide/show functions.
Example, i want an element to be visible on desktop
<div id="test" class="d-none d-md-block">Wele User!</div>
The visibility works as expected. Then when a user clicks a button i want to toggle the visibility (this fails due to the d-md-none class)
$('#btn').on('click',function(){$('#test').hide();});
$('#anotherBtn').on('click',function(){$('#test').show();});
Any idea how to fix this ?
I want to hide some elements on the page that use the class 'd-md-block'. Because of the important on the class definition i can not use jquery hide/show functions.
Example, i want an element to be visible on desktop
<div id="test" class="d-none d-md-block">Wele User!</div>
The visibility works as expected. Then when a user clicks a button i want to toggle the visibility (this fails due to the d-md-none class)
$('#btn').on('click',function(){$('#test').hide();});
$('#anotherBtn').on('click',function(){$('#test').show();});
Any idea how to fix this ?
Share Improve this question edited Aug 27, 2018 at 7:05 user1521944 asked Aug 27, 2018 at 6:45 user1521944user1521944 3371 gold badge2 silver badges14 bronze badges 3-
how do say it fails due to
d-md-block class
? – Thamaraiselvam Commented Aug 27, 2018 at 6:50 -
1
@Thamaraiselvam it actually fails due to
d-none
class it hasdisplay: none !imporatant
property – Ankit Agarwal Commented Aug 27, 2018 at 6:52 - yes correct is due to d-none class but this is not the point... any idea who be able to work with the jquery visibility functions and these classes ? – user1521944 Commented Aug 27, 2018 at 7:04
1 Answer
Reset to default 2There is a simple work around for showing and hiding the element in bootstrap-4. As d-none
class assigns display: none !imporatant
property, you can simply add and remove this class via JavaScript to simulate the hide
and show
operations. Something like this:
$('#btn').on('click', function() {
$('#test').removeClass('d-block')
});
$('#anotherBtn').on('click', function() {
$('#test').addClass('d-block');
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn./bootstrap/4.1.3/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn./bootstrap/4.1.3/css/bootstrap.min.css">
<div id="test" class="d-none d-md-block">Wele User!</div>
<button id='btn'>Hide</button>
<button id='anotherBtn'>Show</button>
本文标签: javascriptbootstrap 4 hide element using jsStack Overflow
版权声明:本文标题:javascript - bootstrap 4 hide element using js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745466983a2659574.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论