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

查看:21
本文介绍了为什么不推荐使用 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?

如许多问题所示,

有很多关于使用 attr 还是使用 prop?"的困惑,而且,根据我(开发人员)的观点,对于 attr() 的所有用途方法,我们可以使用 prop 代替:

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. 性能:John 说访问通过 .attr() 方法访问属性会比直接通过 .prop() 访问它们稍微慢一点";
  3. 改变属性值:所有的都可以通过prop(name,newvalue)方法改变.
  4. 移除属性:所有可以通过removeProp(name)方法移除.
  5. 检查 HTML 属性值:浏览器使用 DOM,所有 HTML 都被转换为 DOM,如果 DOM 受到影响,attr(name) 方法也会受到影响.关于 prop 的强类型":它比html 字符串值"更好(例如checked" vs 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 源代码 及其表示:浏览器在浏览器的内存空间中加载 HTML 页面.好吧,留在内存浏览器的是 DOM 表示,但 DOM 保留了一些原始的东西,如属性cssText 属性,即有时是持久的表示".
    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-property 发生变化时,所有其他 DOM property(如果需要)也会持续变化.

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 vs prop 使用)":

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

  1. 使用 prop,你有 90% 的机会它工作......如果你使用 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天全站免登陆