$('#operatordelivery').attr('checked', true);
您好,我目前正在将 jQuery 版本迁移到 jQuery-2.1.1,我可以在控制台中看到警告JQMIGRATE: jQuery.fn.attr('selected') may use property instead of attribute
。我不清楚这个警告的解释是什么。有人能告诉我这个错误是什么意思吗?
来自 JQMIGRATE 文档:
JQMIGRATE:jQuery.fn.attr('selected') 可以使用属性而不是属性
Cause:在 jQuery 1.9 之前,$().attr("checked") 等有时会
使用checked|selected属性而不是attribute时
与非 XML 元素交互,尽管浏览器和
HTML 规范允许属性(当前状态)有所不同
来自属性(初始/默认状态)。这是从
早期版本的 jQuery 不提供 $().prop。
Solution:布尔属性通常不应传递给
$().attr 根本;替换为 $().prop 除非你真的想要
更新底层 HTML 属性。
Ref: https://github.com/jquery/jquery-migrate/blob/1.x-stable/warnings.md#jqmigrate-jqueryfnattrselected-might-use-property-instead-of-attribute https://github.com/jquery/jquery-migrate/blob/1.x-stable/warnings.md#jqmigrate-jqueryfnattrselected-might-use-property-instead-of-attribute
属性和特性之间的区别在以下方面可能很重要:
具体情况。在 jQuery 1.6 之前,.attr() 方法有时
检索某些属性时考虑属性值,
这可能会导致不一致的行为。从 jQuery 1.6 开始,.prop()
方法提供了一种显式检索属性值的方法,而
.attr() 检索属性。
jQuery 文档:http://api.jquery.com/prop/ http://api.jquery.com/prop/
参考SO的答案:.prop() 与 .attr() https://stackoverflow.com/questions/5874652/prop-vs-attr and jQuery 中 prop() 和 attr() 的区别以及何时使用 attr() 和 prop() https://stackoverflow.com/questions/16051491/difference-between-prop-and-attr-in-jquery-and-when-to-use-attr-and-prop
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)