要使用的getAttribute(),或不使用的getAttribute():这是个问题 [英] To use getAttribute(), or not to use getAttribute(): that is the question

查看:132
本文介绍了要使用的getAttribute(),或不使用的getAttribute():这是个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  的JavaScript的setAttribute属性约VS = 结果
  <一href=\"http://stackoverflow.com/questions/7006253/javascript-dom-how-to-handle-special-properties-as-versus-attributes\">javascript DOM,如何处理&QUOT;特殊性质和QUOT;作为与属性?

很多时候,在论坛或地方,如新闻组我已经被一些(批评我的code时)告诉记者,而不是说,例如 VAR链接= a.href 我应该使用 VAR链接= a.getAttribute('HREF'); 来代替。并利用其互补的的setAttribute()想什么时候分配。

Many times, in forums or places such as Usenet I have been told by some (when criticizing my code) that instead of saying, for example var link = a.href I should use var link = a.getAttribute('href'); instead. And use its complementary setAttribute() when wanting to assign.

他们说,这是做了正确的方式,说我错了,等等等等......我通常不支付任何注意这些。当我问为什么没有人给出了真正的答案。

They say it is the correct way to do it, that I am wrong, blah blah blah... I don’t normally pay any attention to those. And when I ask why nobody gives a real answer.

现在我很好奇在这种情况下,它会更适合使用一个或另一个。

Now I am curious about in which cases it would be more suitable to use one or another.

在什么情况下会更适合说 VAR链接= a.getAttribute('HREF'); 而不是 VAR链接=一个。 HREF

在什么情况下,我shoulw使用的setAttribute()分配,而不是由它的标识符直接将值分配给会员吗?即:`a.href ='someURL';

In what cases would be more suitable to say var link = a.getAttribute('href'); instead of var link = a.href?
And in what cases shoulw I use setAttribute() to assign instead of assigning a value to a member directly by its identifier? i.e: `a.href = 'someURL';

推荐答案

每当有人建议做法,他们应该总是辩解意见。

Whenever someone recommends a practice they should always justify the advice.

不使用原因的的getAttribute 的setAttribute 的比IE浏览器版本直至并包括8至少有虫子在实施这些DOM方法。此外,浏览器在如何改变响应的使用的 GET /的setAttribute的DOM属性的差异。

Reasons for not using getAttribute and setAttribute are than IE versions up to and including 8 at least have bugs in their implementation of those DOM methods. Also, browsers have differences in how they change DOM properties in response to the use of get/setAttribute.

不过,浏览器是对于DOM属性非常一致,所以它是更简单,如果你使用DOM属性来编写跨浏览器code。唯一需要注意的是,有些浏览器不创建非标准HTML属性DOM属性,但他们都将设置他们使用属性。

However, browsers are remarkably consistent in regard to DOM properties, so it is much simpler to write cross browser code if you use DOM properties. The only caveat is that some browsers do not create DOM properties for non-standard HTML attributes, but they will all set them using properties.

另外一个好处是,DOM属性访问比使用函数调用的 GET /的setAttribute

An added bonus is that DOM property access is much faster than using a function call to get/setAttribute.

HTML5试图标准化一些这些行为,但与HTML5中的问题是,它不是一个W3C标准(和可能永远是),它是指企图什么浏览器做不仅文档活规范 (或多或少)还能有什么它的作者想他们没有区分两者的事情。因此,尽管它是作为一种有益的所构建的规范加愿望清单,这是作为一个标准挺有用的。

HTML5 is attempting to standardise some of these behaviours, but the problem with HTML5 is that it is not a W3C standard (and may never be) and that it is a "living specification" that attempts to not only document what browsers do (more or less) but also what its authors would like them to do without distinguishing between the two. So while it is helpful as a kind of "as built" specification plus wish list, it is quite useless as a standard.

哎呀,我才不小心包括HTML5夸大其词?

Ooops, did I accidentally include a rant on HTML5?

这篇关于要使用的getAttribute(),或不使用的getAttribute():这是个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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