[英]How to change class of button on click
I am trying to change the icon of a button when clicked. 我正在尝试单击时更改按钮的图标。
With the following code I have managed to change the icon when first clicked, but it doesn't change back when clicked again. 通过以下代码,我设法在首次单击时更改了图标,但是再次单击时并没有更改。
HTML HTML
<button class="toggle-button custom-navbar-toggle">
<i class="material-icons">menu</i>
</button>
JS JS
$('.custom-navbar-toggle').click(function(){
var $this = $(this);
if($this.hasClass('not-clicked')){
$this.html('<i class="material-icons clicked">menu</i>');
} else {
$this.html('<i class="material-icons not-clicked">check</i>');
}
});
How can I get the button to change back once clicked again? 再次单击该如何使按钮变回原来的状态?
A better approach is addClass()
and removeClass()
like below:- 更好的方法是
addClass()
和removeClass()
如下所示:
$('.custom-navbar-toggle').click(function(){
if($(this).children('i').hasClass('not-clicked')){
$(this).children('i').removeClass('not-clicked').addClass('clicked').html('menu');
} else {
$(this).children('i').removeClass('clicked').addClass('not-clicked').html('check');
}
});
Example:- 例:-
$('.custom-navbar-toggle').click(function(){ if($(this).children('i').hasClass('not-clicked')){ $(this).children('i').removeClass('not-clicked').addClass('clicked').html('menu'); } else { $(this).children('i').removeClass('clicked').addClass('not-clicked').html('check'); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="toggle-button custom-navbar-toggle"> <i class="material-icons">menu</i> </button>
Note:- your initial code is adding class to children <i>
, so you have to check class also on children itself, not on button. 注意:-您的初始代码是将类添加到子类
<i>
,因此您还必须在子类本身而不是按钮上检查类。
So you have to do it like below:- 所以你必须像下面这样:
$('.custom-navbar-toggle').click(function(){
if($(this).children('i').hasClass('not-clicked')){
$(this).html('<i class="material-icons clicked">menu</i>');
} else {
$(this).html('<i class="material-icons not-clicked">check</i>');
}
});
EXAMPLE:- 例:-
$('.custom-navbar-toggle').click(function(){ if($(this).children('i').hasClass('not-clicked')){ $(this).html('<i class="material-icons clicked">menu</i>'); } else { $(this).html('<i class="material-icons not-clicked">check</i>'); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="toggle-button custom-navbar-toggle"> <i class="material-icons">menu</i> </button>
Give "not-clicked" class by default. 默认情况下提供“未单击”类。
<button class="toggle-button custom-navbar-toggle">
<i class="material-icons not-clicked">menu</i>
</button>
Then using toggerClass function you can add remove class names to i tag 然后使用toggerClass函数,您可以将删除类名称添加到i标签
$('.custom-navbar-toggle').click(function(){
$(this).find("i").toggleClass("not-clicked").toggleClass("clicked");
});
In your code $this
only target $('.custom-navbar-toggle')
which is the parent of the i
, you need go down to the child to call .hasClass
在您的代码
$this
仅$this
目标$('.custom-navbar-toggle')
是i
的父级,您需要转到该子级以调用.hasClass
use find('i')
or children() 使用
find('i')
或children()
$('.custom-navbar-toggle').click(function() { var $this = $(this); if ($this.find('i').hasClass('not-clicked')) { $this.html('<i class="material-icons clicked">menu</i>'); } else { $this.html('<i class="material-icons not-clicked">check</i>'); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="toggle-button custom-navbar-toggle"> <i class="material-icons">menu</i> </button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.