为什么不推荐使用jQuery 1.9+ attr()方法? [英] Why jQuery 1.9+ attr() method not deprecated?

查看:361
本文介绍了为什么不推荐使用jQuery 1.9+ attr()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是jQuery1.9 +软件开发人员,可以在日常工作中弃用" attr()方法的使用吗?

Can I, a jQuery1.9+ software developer, "deprecate" the use of the attr() method in my day-by-day work?

如许多问题所示,

  • .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:

  1. 向后兼容:对新软件进行编码不需要此;
  2. 性能:约翰说通过.attr()方法访问属性比通过.prop()直接访问属性要慢一些.
  3. 更改属性值:都可以通过prop(name,newvalue)方法更改.
  4. 删除属性:都可以通过removeProp(name)方法删除.
  5. 检查HTML属性值:浏览器使用DOM,所有HTML都已转换为DOM,并且,如果DOM影响了attr(name)方法,那么也会受影响.关于道具的强类型":它比"html字符串值"(例如"checked"与 true )好.
  6. 检查是否在"HTML原始代码"中定义了属性(假设您浏览器中的方法attr如果未定义,则返回 undefined )...好吧,我们需要一些软件吗? 在表单,.val()方法是推荐的jQuery获取或设置表单值的方法"
  7. 跨浏览器一致性:两者(不仅是attr)都是一致的方法. (是?).
  1. backward-compatibility: coding new software not need this;
  2. performance: John says "Accessing properties through the .attr() method will be slightly slower than accessing them directly through .prop()";
  3. Change attribute value: all can be changed by the prop(name,newvalue) method.
  4. Remove attribute: all can be removed by the removeProp(name) method.
  5. 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).
  6. 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"
  7. 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 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.

牢记这些概念,我们可以绘制"经验法则(针对attrprop的用法)":

With these concepts in mind, we can draw a "rule of thumb (for attr vs prop uses)":

  1. 使用prop,您有90%的机会可以使用它... 99%(如果您使用jQuery进行更改DOM状态(和标准DOM 假设)
  2. 如果您需要更改或访问data-*属性,请使用.data()方法.
  3. 否则,(非DOM,非数据或非标准的东西)检查是否有使用.attr()的情况,并保留一些时间来研究它(请参见上面的答案).
  1. Use prop, you have 90% of chances that it works... 99% if you using jQuery for change DOM states (and standard DOM hypothesis).
  2. If you need to change or access to data-* attributes, use .data() method.
  3. 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屋!

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