admin管理员组文章数量:1287856
Forgive me for being a noob, but shouldn't this work?
$(document).ready(function() {
$('.button').click(function() {
$(this).addClass('button-clicked');
});
$('.button-clicked').click(function() {
$(this).removeClass('button-clicked');
});
});
Shouldn't the second click remove the class and take it back to .button?
Here it is on jsfiddle: /
Forgive me for being a noob, but shouldn't this work?
$(document).ready(function() {
$('.button').click(function() {
$(this).addClass('button-clicked');
});
$('.button-clicked').click(function() {
$(this).removeClass('button-clicked');
});
});
Shouldn't the second click remove the class and take it back to .button?
Here it is on jsfiddle: http://jsfiddle/pXdwM/
Share Improve this question edited Dec 9, 2010 at 7:08 Yi Jiang 50.2k16 gold badges138 silver badges136 bronze badges asked Dec 9, 2010 at 7:00 izolateizolate 1,6004 gold badges22 silver badges37 bronze badges2 Answers
Reset to default 9no, because at the point you're calling the second click()
the button doesn't have ".button-clicked" and therefore event handler is not assigned. You could rewrite it like this
$('.button').click(function() {
$(this).toggleClass('button-clicked');
});
or use live()
$('.button-clicked').live("click", function() {
$(this).removeClass('button-clicked');
});
You are adding an event to each element with class '.button-clicked', but the class does not apply until you actually click. So you need to move the second listener into the first callback, or use the toggleClass
function:
$('.button').click(function() {
$(this).toggleClass('button-clicked');
});
本文标签: javascriptremoveClass doesn39t work how addClass doesStack Overflow
版权声明:本文标题:javascript - .removeClass doesn't work how addClass does? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741309390a2371562.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论