$("div").attr("id", function (arr) {
return "div-id" + arr;
}).each(function () {
$("span", this).html("(ID = '<b>" + this.id + "</b>')");
});
里面的this.id是获取对应id的属性值,但是为什么换了class就不行了。只有id才能这么写么?
$("div").attr("id", function (arr) {
return "div-id" + arr;
}).each(function () {
$("span", this).html("(ID = '<b>" + this.id + "</b>')");
});
里面的this.id是获取对应id的属性值,但是为什么换了class就不行了。只有id才能这么写么?
1 回答572 阅读
1 回答572 阅读
这里的this其实并不是jq对象,而是DOM节点对象,其上的
id
属性就是标签上的id
,但是其classList
属性才是class
的伪数组。补充:
这个是
this
对象的部分截图,其中
classList
和className
都是class
属性相关的属性,伪数组就是指那些具有
length
属性,或是按照数字下标进行存放数据,但是不具备数组方法的对象。