可能重复:
JavaScript setAttribute vs .attribute=
javascript dom,如何处理特殊属性"与属性相比?
很多时候,在论坛或诸如 Usenet 之类的地方,一些人(在批评我的代码时)告诉我,而不是说,例如 var link = a.href
我应该使用 var link = 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 link = a.getAttribute('href');
代替 var link = a.href
更合适?
在什么情况下我应该使用 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 方法的实现中存在错误.此外,浏览器在如何更改 DOM 属性以响应 get/setAttribute 的使用方面也存在差异.
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 属性,编写跨浏览器代码要简单得多.唯一需要注意的是,一些浏览器不会为非标准 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.
W3C 已经发布 HTML 5 规范 有一段时间了(包括编号版本和列表更改),并且 WHATWG 还发布 HTML 和 DOM 标准几乎每天更新,但没有说明发生了什么变化.一个或另一个最终可能会放弃.
The W3C has been publishing HTML 5 specifications for a while now (including numbered versions and lists of changes), and the WHATWG also publishes HTML and DOM standards with almost daily updates but without indicating what has changed. One or the other may give up eventually.
这篇关于使用getAttribute(),还是不使用getAttribute():这是个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!