我希望每当我在按钮上添加按钮时,都应该添加一个类,并在下一次单击后删除类。 我尝试使用以下代码,但第二次单击时未删除类,这是我的代码。
$("#hide_show_keypad").click(function (e) {
$(this).addClass("fcurrent").siblings().removeClass("fcurrent");
});
<input type="submit" name="btn" id="hide_show_keypad" class="mytest" >
答案 0 :(得分:0)
使用toggleClass()
使它按需工作。
$("#hide_show_keypad").click(function(e) {
$(this).toggleClass("fcurrent")
});
演示
$("#hide_show_keypad").click(function(e) {
$(this).toggleClass("fcurrent")
});
.fcurrent{color:blue;background-color:yellow}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="submit" name="btn" id="hide_show_keypad" class="mytest" />
答案 1 :(得分:0)
$('#btn').click(function() {
$('#paragraph').toggleClass("style");
});
.style {
background-color: #000;
color: cyan;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="paragraph">This is a sample paragraph.</p>
<br/>
<button type="button" id="btn">Click Me</button>