admin管理员组文章数量:1334887
I'd like to be able to remove one class from a div and add another upon the click of a button. But I can't get it to work.
<div class="hiddennav displaynone">
<ul>
<?php wp_nav_menu(array('menu' => 'Main Nav menu')); ?>
</ul>
</div> <!-- end div hiddennav -->
<div class="fixednav">
<div class="shownav"><a href="#" class="shownavbutton"></a></div>
<!-- end div shownav -->
</div> <!-- end div fixednav -->
Here's the jQuery:
$(document).ready(function(){
$(".shownavbutton").click(function() {
$(".hiddennav").removeClass("displaynone").addClass("displayblock");
});
I'd preferably want it to toggle the classes too when clicked multiple times.
I'd like to be able to remove one class from a div and add another upon the click of a button. But I can't get it to work.
<div class="hiddennav displaynone">
<ul>
<?php wp_nav_menu(array('menu' => 'Main Nav menu')); ?>
</ul>
</div> <!-- end div hiddennav -->
<div class="fixednav">
<div class="shownav"><a href="#" class="shownavbutton"></a></div>
<!-- end div shownav -->
</div> <!-- end div fixednav -->
Here's the jQuery:
$(document).ready(function(){
$(".shownavbutton").click(function() {
$(".hiddennav").removeClass("displaynone").addClass("displayblock");
});
I'd preferably want it to toggle the classes too when clicked multiple times.
Share Improve this question edited Oct 12, 2012 at 17:16 dda 6,2132 gold badges27 silver badges35 bronze badges asked Oct 12, 2012 at 17:12 andyandy 2,7548 gold badges49 silver badges63 bronze badges 2- 4 you got far enough to add "want it to toggle the classes" , but not far enough to google "jQuery toggle class" because you'd find toggleClass() – Scott Selby Commented Oct 12, 2012 at 17:15
-
1
Looks like you're missing an extra
});
to end the$(document).ready()
function. – Marcus Commented Oct 12, 2012 at 17:17
2 Answers
Reset to default 5Try $(selector).toggleClass(class)
:
$(document).ready(function(){
$(".shownavbutton").click(function() {
$(".hiddennav").toggleClass("displaynone").toggleClass("displayblock");
});
});
Optionally, you could use the CSS method as well (assuming that this is all you're acplishing via your displaynone
and displayblock
classes):
$(".hiddennav").toggle(function() {
$(this).css('display','none');
}, function() {
$(this).css('display','block');
});
Try this
$(".hiddennav").toggleClass("displaynone displayblock");
本文标签: javascriptRemoveadd class on click of a buttonStack Overflow
版权声明:本文标题:javascript - Removeadd class on click of a button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742371389a2462306.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论