問題描述
可能重復:
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():這是個問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!