在 jquery 中引用此变量的差异
Difference in referring this variable in jquery
我对下面代码中的这个变量感到困惑。
jQuery.fn.extend({
check: function() {
return this.each(function() {
this.checked = true;
});
},
uncheck: function() {
return this.each(function() {
this.checked = false;
});
}
});
// Use the newly created .check() method
$( "input[type='checkbox']" ).check();
请告诉我哪个 this 指的是哪个对象。
在this.each
中,this
指的是提供的jQuery对象中元素的集合。在您的示例中,它将是 input[type="checkbox"]
元素的 all。
在 this.checked
中,this
是 each
循环迭代中的单个 DOMElement。
我对下面代码中的这个变量感到困惑。
jQuery.fn.extend({
check: function() {
return this.each(function() {
this.checked = true;
});
},
uncheck: function() {
return this.each(function() {
this.checked = false;
});
}
});
// Use the newly created .check() method
$( "input[type='checkbox']" ).check();
请告诉我哪个 this 指的是哪个对象。
在this.each
中,this
指的是提供的jQuery对象中元素的集合。在您的示例中,它将是 input[type="checkbox"]
元素的 all。
在 this.checked
中,this
是 each
循环迭代中的单个 DOMElement。