JQMIGRATE:jQuery.fn.attr('selected')可以使用属性而不是属性 [英] JQMIGRATE: jQuery.fn.attr('selected') may use property instead of attribute

查看:356
本文介绍了JQMIGRATE:jQuery.fn.attr('selected')可以使用属性而不是属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$('#operatordelivery').attr('checked', true);

您好,我目前正在将jQuery版本迁移到jQuery-2.1.1,可以在控制台JQMIGRATE: jQuery.fn.attr('selected') may use property instead of attribute中看到警告.对于该警告的解释,我并不清楚.有人可以告诉我这个错误是什么意思吗?

Hi, I am currently working on migrating jQuery version to jQuery-2.1.1 where I could see the warning in the console JQMIGRATE: jQuery.fn.attr('selected') may use property instead of attribute. I didn't get a clear idea of what this warning explains. Can somebody tell me what does this error means?

推荐答案

来自JQMIGRATE文档:

From JQMIGRATE docs:

JQMIGRATE:jQuery.fn.attr('selected')可能使用属性而不是属性

原因:在jQuery 1.9之前,$().attr("checked")等有时会 在以下情况下,使用checked | selected属性而不是属性 尽管浏览器和 HTML规范允许属性(当前状态)不同 从属性(初始/默认状态)中获取.这是来自 不提供$().prop的jQuery早期版本.

JQMIGRATE: jQuery.fn.attr('selected') may use property instead of attribute

Cause: Prior to jQuery 1.9, $().attr("checked") etc. would sometimes use the checked|selected property instead of the attribute when interacting with non-XML elements, despite the fact that browsers and the HTML specifications allow the properties (current state) to differ from the attributes (initial/default state). This was a holdover from earlier versions of jQuery that did not offer $().prop.

解决方案:通常不应将布尔属性传递给 $().attr完全;除非您确实打算使用$().prop替换 更新基础HTML属性.

Solution: Boolean properties should generally not be passed to $().attr at all; replace with $().prop unless you truly intend to update the underlying HTML 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()检索属性.

The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes.

jQuery文档: http://api.jquery.com/prop/

jQuery docs: http://api.jquery.com/prop/

SO的参考答案: .prop()与.attr()

Referenced answer of SO: .prop() vs .attr() and difference between prop() and attr() in jQuery and when to use attr() and prop()

这篇关于JQMIGRATE:jQuery.fn.attr('selected')可以使用属性而不是属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆