ITPub博客

首页 > IT职业 > IT职场 > jQuery的prop和attr方法比较

jQuery的prop和attr方法比较

IT职场 作者:joytoy 时间:2021-09-09 09:13:07 0 删除 编辑

JQuery.attr():
Get the value of an attribute for the first element in the set of matched elements.

JQuery. Prop():
Gets the value of a property for the first element in the set of matched elements.

 

Reference MSDN:
for a checkbox (jquery 1.6+)

 

.attr('checked')     //returns checked
.prop('checked')     //returns true
.is(':checked')     //returns true

 

Prop() method returns Boolean value for checked, selected, disabled, readOnly..and so on while attr returns defined string. So, you can directly use .prop("checked") in if condition. SelectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, and defaultSelected..and so on should be retrieved and set with the .prop() method. These do not have corresponding attributes and are only properties. .attr() calls .prop() internally so .attr() method will be slightly slower than accessing them directly through .prop().


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4650/viewspace-2800685/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2018-08-12

  • 博文量
    97
  • 访问量
    18298