jquery的attr和prop区别之实例

  • 对于HTML元素本身就带有的固有属性,在处理时,使用prop方法。
  • 对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法。

html文件

<tr>
                        <th></th>
                        <td>
                            <a class="easyui-linkbutton" style="60px;" onclick="selectAll()">全选</a>
                            <a class="easyui-linkbutton" style="60px;" onclick="unSelectAll()">全不选</a>
                        </td>
                    </tr>

JS文件

//给相同名称的CheckBox赋属性值

function selectAll(){ $(
'input[name="groupItem"]').val(function(index,val){ $(this).prop('checked',true);
          //$(this).attr('checked',true);//错误
return val; }) } function unSelectAll(){ $('input[name="groupItem"]').val(function(index,val){ $(this).prop('checked',false);
//$(this).attr('checked',false);错误
return val; }) }
原文地址:https://www.cnblogs.com/mr-wuxiansheng/p/6863941.html