JavaScript的document對象

編程語言 JavaScript HTML Links 下班很無聊 2017-06-23

document

一、屬性

A、title-- 返回或設置當前文檔的標題

alert(document.title);

document.title="暴雨轉大雨";

alert(document.title);//暴雨轉大雨

B、URL--返回當前文檔的url

alert(document.URL);

alert(location.href);

C、bgcolor--設置文檔的背景色

document.bgColor="blue";

D、fgColor--設置文檔的前景色(文字的顏色)

document.fgColor="red";

二、方法

A、getElementById(idname)--返回擁有指定id的(第一個)對象的引用

B、getElementsByTagName(tagname)--返回指定標籤名稱的對象的集合

var ps=document.getElementsByTagName("p");

var lengths=ps.length;

alert(lengths);

for (var i = 0; i < lengths; i++) {

alert(ps[i].innerHTML);

};

C、getElementsByName(name)--返回帶有指定name對象的集合

注:getElementsByName(name)是不兼容的。

如果是IE:

若該對象的標準屬性包含name,那麼可以正確使用。否則不可以使用。

如果是FF:

該方法適用於任何情況。

結論:該方法主要適用於表單

D、getElementByClassName(classname)--返回帶有指定classname對象的集合

E、 write()--直接在html中寫內容

三、document對象的集合

A、all--所有元素的集合

alert(document.all);//[object HTMLAllCollection]

B、forms--返回文檔中所有form對象的引用

alert(document.forms.length);

通過集合來訪問相應的對象

(1)通過下標的形式

alert(document.forms[0].name);

(2)通過那麼屬性來訪問

alert(document.forms["form2"].text2.value);

C、.anchors錨--返回文檔中所有anchors對象的引用

D、images 返回對文檔中所有image 對象的引用

E、

JavaScript的document對象

links 返回對文檔中所有area 對象和link對象的引用

相關推薦

推薦中...