为什么不推荐使用jQuery 1.9+ attr()方法? [英] Why jQuery 1.9+ attr() method not deprecated?
问题描述
我是jQuery1.9 +软件开发人员,可以在日常工作中弃用" attr()
方法的使用吗?
Can I, a jQuery1.9+ software developer, "deprecate" the use of the attr()
method in my day-by-day work?
如许多问题所示,
- .prop()与.attr()(主要)
- jQuery attr vs. prop,有道具列表吗?
- jQuery attr vs prop?
- 将jQuery 1.8.3迁移到1.9.0-替换过时的.attr()
- ...等等...
- .prop() vs .attr() (main)
- jQuery attr vs. prop, there are a list of props?
- jQuery attr vs prop?
- Migrating jQuery 1.8.3 to 1.9.0 - Replacing deprecated .attr()
- ... etc. ...
关于使用attr还是使用prop?"存在很多困惑,并且,从我(开发人员)的角度来看,对于attr()
方法的所有用法,我们都可以使用
there are a lot of confusion about "use attr or use prop?", and, by my (developer's) view point, for all uses of attr()
method, we can use prop
instead:
- 向后兼容:对新软件进行编码不需要此;
- 性能:约翰说通过.attr()方法访问属性比通过.prop()直接访问属性要慢一些.
- 更改属性值:都可以通过
prop(name,newvalue)
方法更改. - 删除属性:都可以通过
removeProp(name)
方法删除. - 检查HTML属性值:浏览器使用DOM,所有HTML都已转换为DOM,并且,如果DOM影响了
attr(name)
方法,那么也会受影响.关于道具的强类型":它比"html字符串值"(例如"checked"与 true )好. - 检查是否在"HTML原始代码"中定义了属性(假设您浏览器中的方法
attr
如果未定义,则返回 undefined )...好吧,我们需要一些软件吗? 在表单,.val()方法是推荐的jQuery获取或设置表单值的方法" - 跨浏览器一致性:两者(不仅是attr)都是一致的方法. (是?).
- backward-compatibility: coding new software not need this;
- performance: John says "Accessing properties through the .attr() method will be slightly slower than accessing them directly through .prop()";
- Change attribute value: all can be changed by the
prop(name,newvalue)
method. - Remove attribute: all can be removed by the
removeProp(name)
method. - Check HTML attribute values: browser use DOM, all HTML was converted to DOM, and, if DOM affected the
attr(name)
method also affected. About "strong type" of prop: it is better than "html string value" (ex. "checked" vs true). - Check if an attribute was defined in the "HTML original code" (supposing that method
attr
in your browser returns undefined if it is not)... Well, we need this in some piece of software? At forms, ".val() method is the recommended jQuery way to get or set the values of form" - Cross-browser consistency: both (not only attr) are consistent methods. (it is??).
因此,在此时(2013年),在开发新的jQuery代码时,我没有看到使用attr
方法的充分理由...但是,换句话说,这就是问题:那里在我的日常任务中使用attr
方法的充分理由吗?
So, at this time (2013), I not see a good reason to use attr
method when developing new jQuery code... But, well, this is, in other words, the question: There are a good reason to use attr
method in my day-by-day tasks?
推荐答案
我的作业...阅读@cHao和@cernunnos很好的解释,并在使用了几个月后... 我想作为程序员,我们需要另一个概念来决定何时使用prop和attr:
My homework... After read @cHao and @cernunnos good explanations, and after some months using it... I thing we need another concept to decide, as programmer, when use or not prop and attr:
-
HTML源代码及其表示形式:浏览器将HTML页面加载到浏览器的内存空间中.嗯,留在内存中的浏览器是DOM表示形式,但是DOM保留了一些原始内容,如属性和 PS:在标准DOM"中未定义的属性没有与 DOM属性的映射.当一个(非只读)属性发生更改时,如果有对应的(映射)属性,则可以自动对其进行更改.
HTML source-code and its representation: the browser loads HTML page in the browser's memory space. Well, what stay at memory's browser is the DOM representation, but DOM preserves some original things, as attributes and cssText property, that are "sometimes persisted representations".
PS: attributes that are not defined in the "standard DOM", have not a mapping to DOM properties. When one (not read-only) attribute changes, if there are corresponding (mapped) property, it can be changed automaticlally.
DOM状态:动态页面需要直接在DOM上进行更改,这就是最终用户所看到的.因此,如果页面上有一些单击并更改"操作,则每次DOM转到新的页面表示形式时,因此,每次DOM具有新的
DOM state: dynamical pages need changes directly at the DOM, that is what final users see. So, if there are some "click and change" actions at the page, each time the DOM goes to a new page representation, so, each time DOM have a new state.
When one DOM-property changes, all other DOM properties (if need) changes consistently.
牢记这些概念,我们可以绘制"经验法则(针对attr
与prop
的用法)":
With these concepts in mind, we can draw a "rule of thumb (for attr
vs prop
uses)":
- 使用
prop
,您有90%的机会可以使用它... 99%(如果您使用jQuery进行更改DOM状态(和标准DOM 假设) - 如果您需要更改或访问
data-*
属性,请使用.data()
方法. - 否则,(非DOM,非数据或非标准的东西)检查是否有使用
.attr()
的情况,并保留一些时间来研究它(请参见上面的答案).
- Use
prop
, you have 90% of chances that it works... 99% if you using jQuery for change DOM states (and standard DOM hypothesis). - If you need to change or access to
data-*
attributes, use.data()
method. - Otherwise (non-DOM, non-data, or non-standard things) check if a case of use
.attr()
, and reserve some time to study it (see answers above).
这篇关于为什么不推荐使用jQuery 1.9+ attr()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!