下一个实际可用的函数,从最佳实践角度来看是否不好?IDE正在警告我:
“潜在的无效使用'this'。检查JavaScript中'this'是否在同一闭包或外部内容中。”
当我勾选一个ID为
“潜在的无效使用'this'。检查JavaScript中'this'是否在同一闭包或外部内容中。”
$(document).on('change', '#select-all', function(){
if( this.checked ) {
$(this).closest('table').find('input[name="row-id"]').each( function() {
this.checked = true; // Here
})
}
else {
$(this).closest('table').find('input[name="row-id"]').each( function() {
this.checked = false; // Here
});
}
});
当我勾选一个ID为
select-all
的复选框时,它会将所有其他复选框都标记为已选择。
prop()
中,this.checked
是指'#select-all'
的this
,还是.find()
的this
? - JorgeeFGthis
将指向 ID 为select-all
的元素。 - VisioN