2013-04-07 83 views
2

下面是一些代碼的document.getElementById VS dom.byid

var docDiv= document.getElementById("divId"); 

    var dojoDiv= dom.byId("divId"); 

是什麼JavaScript的的document.getElementById和道場的dom.byid之間的差異。這是一個更快。如果你想使用dom,我們需要加載dojo.js.

+0

'document.getElementById(「divId」)'更快! – rab 2013-04-07 06:02:36

+0

爲什麼document.getElementById更快?哪一個是我應該使用的? – 2013-04-07 06:04:53

+0

因爲'dom.byId()'是本地JS的一個包裝'document.getElementById()' – 2013-04-07 06:05:32

回答

0

document.getElementById()dom.byId()更快。因爲dom.byId()需要加載dojo核心文件。

1

我認爲document.getElementById()dom.byId()快,因爲dojo內部使用 document

0

從道場的GitHub代碼https://github.com/dojo/dojo/blob/master/dom.js#L51,使用document.getElementById它採用document.getElementById內部

dom.byId = function(id, doc){ 
    // inline'd type check. 
    // be sure to return null per documentation, to match IE branch. 
    return ((typeof id == "string") ? (doc || win.doc).getElementById(id) : id) || null; // DOMNode 
}; 

,我們能夠避免調用這個函數dom.byId!..但是性能差異是非常少的

我喜歡dom.byId,因爲它使用時間短。否則我必須在每一處寫上冗長的document.getElementById

2

這裏是Dojo的dom.byId的非IE版本:

dom.byId = function(id, doc){ 
      // inline'd type check. 
      // be sure to return null per documentation, to match IE branch. 
      return ((typeof id == "string") ? (doc || win.doc).getElementById(id) : id) || null; // DOMNode 
     }; 

正如你會注意到它使用的getElementById。

希望這回答你的問題。

相關問題