1.全选
<table cellpadding="0" cellspacing="0" border="0" class="stdtable stdtablecb">
<thead>
<tr>
<th><input type="checkbox" class="checkall" onclick="selectAll($(this))" /></th>
<th>编号</th>
<th>名称</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" /></td>
<td>1</td>
<td>wj</td>
</tr>
</tbody>
</table>
function selectAll(obj){
// 找到tbody下input(input只有checkbox),或者html中input加上class,进行获取
let tdInput=obj.parents("thead").next().find("input");
if(obj.is(':checked')){
obj.prop('checked',true);
tdInput.prop('checked',true);
}else{
obj.prop('checked',false);
tdInput.prop('checked',false);
}
}
2.$(this)与this的区别
关于这两个,之前一直迷迷糊糊的。索性查了一下,使用起来也清晰明朗
$(this)是Jquery中的对象,而this是html中的元素,它相当于一个指针。
this可以调用的方法都是原生js中的
$(this)是个转换,将this表示的dom对象转为jquery对象,这样就可以使用jquery提供的方法操作
alert($(this)); 弹出的结果是[object Object ]
alert(this); 弹出来的是[object HTMLImageElement]