有人可以帮助我了解使用 jQuery
this
关键字的语法
吗?
这是我的有效代码:
var obj = jQuery.parseJSON(data);
$('.example_infobox1').addClass(obj.gridlayout);
$('.example_infobox1 .info-box').addClass(obj.boxcolor);
$('.example_infobox1 .info-box-icon').addClass(obj.iconcolor);
$('.example_infobox1 i').addClass(obj.icon);
$('.example_infobox1 .info-box-text').html(obj.text);
$('.example_infobox1 .info-box-number').html(obj.number);
这是我正在编写的代码:
var obj = jQuery.parseJSON(data);
$('.example_infobox1')
{
$(this).addClass(obj.gridlayout);
$('.info-box', this).addClass(obj.boxcolor);
$('.info-box-icon', this).addClass(obj.iconcolor);
$('i', this).addClass(obj.icon);
$('.info-box-text', this).html(obj.text);
$('.info-box-number', this).html(obj.number);
}
我在控制台中没有收到任何错误,但是 html 内容的格式不正确。
谢谢
最佳答案
我认为在这种情况下使用变量会更好。
var box = $('.example_infobox1');
box.addClass(obj.gridlayout);
$('.info-box', box).addClass(obj.boxcolor);
$('.info-box-icon', box).addClass(obj.iconcolor);
$('i', box).addClass(obj.icon);
$('.info-box-text', box).html(obj.text);
$('.info-box-number', box).html(obj.number);
关于jquery - 将 this 关键字与 jQuery 选择器一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30701732/