- attr ,prop
给元素设置/获取属性
设置时可以给所有元素设置属性
获取时只能获取第一个元素的属性
attr设置普通属性
prop设置特殊属性 (不是所有标签都有的属性) - 全选/ 全不选 点击相应按钮时 为checkbox设置属性
$(':checkbox').attr('checked', true/false)
- 反选
(
因attr ,prop获取时只能获取第一个元素的属性,所以用each( function(index, Element){ } )
)
iquery方法
$('#reverse').click(function(){
$(':checkbox').each( function(index, Element){
var tag = $(Element).prop('checked')
$(Element).prop('checked',!tag)
} )
})
dom方法
$('#reverse').click(function(){
$(':checkbox').each( function(index, Element){
Element.checked=!Element.checked
} )
})