2011-04-08 51 views
4

任何一個可以解釋的是什麼差異[0]

document.all 

document.forms[0]請區別?

謝謝

回答

5

document.all給你到含有在Internet Explorer(IE)文檔的所有元素的數組狀對象的引用。 Document.forms[0]給你一個指向文檔中的第一個表單元素的指針,在所有瀏覽器中

這兩個是非常不同的。如果您的表單有名稱屬性,請說'myform',那麼在IE中表單可以用document.all.myform

來引用document.all已從IE版本5及以上版本棄用。你仍然可以使用它,即使在IE9中它仍然可用。通常它被用來測試,如果瀏覽器是IE:

if (document.all) { 
    //o no, it's IE again! We have to do it another way! 
} 

document.forms[0]形式引用的形式被認爲是不好的做法。更多,可以發現here

注:由於這個答案是第一次寫IE11已經介紹了下降的更多信息的document.all See Compatibility changes in IE11支持

+0

@Kooilnc所以這兩種方法纔有用在IE瀏覽器?假設我們想要在Chrome或Safari中運行相同的代碼做什麼? – Mihir 2011-04-08 05:43:55

+0

@Mhhir:bottom line =你可以在Internet Explorer中使用'document.all',你只會在Internet Explorer <5中明確需要它。所以'document.forms'可用於所有瀏覽器,包括IE。 – KooiInc 2011-04-08 05:48:01

+0

@Kooilnc oh..cool謝謝 – Mihir 2011-04-08 05:48:59

3

該物業的document.all是所有的數組文檔中的HTML元素。而Document.forms [0]是文檔中的第一個表單。你應該避免使用document.all。

Internet Explorer 4中引入了 的document.all DOM(文檔對象模型 ),以允許各 部分網頁的訪問。在此之後不久, 標準DOM方法getElementById 被引入,因此 可用於所有版本5+瀏覽器。 這意味着的document.all 引用需要 支持IE4.Just大約無人運行IE4 任何更多的 的document.all DOM支持這樣不再需要 。

要了解有關如何使用document.form [0]的更多信息,請參閱this

0

首先,您使用document.all.myform來訪問您的表單。 第二格式,你可以使用document.forms [0]訪問您的形式

 both one two is same in function but two is better than one 
          document.all.myform using this your searching all the way rome.