[英]jquery addClass and removeClass issues
I am having strange issues with jQuery and adding and removing classes. 我在jQuery以及添加和删除类方面遇到了奇怪的问题。 I'm trying to see on success of a json request, that for the particular hyper link, it should call addClass
and removeClass
to add/remove particular css properties. 我试图看到成功的json请求,对于特定的超链接,它应该调用addClass
和removeClass
来添加/删除特定的CSS属性。 When I click on it, it NEVER works, but when I try the css classes independently, they work fine. 当我单击它时,它永远都行不通,但是当我独立尝试css类时,它们可以正常工作。 Is there something I'm missing here? 我在这里想念什么吗? Thanks for the input. 感谢您的输入。
$(document).ready(function() {
$('.add_link').bind("click", function(e) {
$.getJSON("/add/", function(json) {
if (json.SUCCESS != null) {
$(this).removeClass('blue_button_link').addClass('gray_out_button_link');
}
});
});
});
$(document).ready(function() {
$('.add_link').bind("click", function(e) {
// cache it in a local variable.
var $this = $(this);
$.getJSON("/add/", function(json) {
if (json.SUCCESS != null) {
$this.removeClass('blue_button_link').addClass('gray_out_button_link');
}
});
});
});
In event handler you have another context, so you cannot use this
how you want. 在事件处理程序中,您具有另一个上下文,因此您无法按需使用this
。 Try: 尝试:
$(document).ready(function() {
var link = $('.add_link');
link.bind("click", function(e) {
$.getJSON("/add/", function (json) {
if (json.SUCCESS != null) {
link.removeClass('blue_button_link').addClass('gray_out_button_link');
}
});
});
});
Or: 要么:
$(document).ready(function() {
$('.add_link').bind("click", function(e) {
var link = $(this);
$.getJSON("/add/", function (json) {
if (json.SUCCESS != null) {
link.removeClass('blue_button_link').addClass('gray_out_button_link');
}
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.