JavaScript document

window -- document用於表現HTML頁面當前窗體的內容javascript

  • document,中文"文檔"
  • document是BOM中最重要對象之一
  • document對象是window對象的屬性
  • document對象包含一個節點對象,此對象包含每一個單獨頁面的全部HTML元素,這就是W3C的DOM對象。

document屬性

  • cookie -- 用戶cookie
  • title -- 當前頁面title標籤中定義的文字
  • URL -- 當前頁面的URL

下面內容的不建議使用css

  • alinkColor -- 表明HTML body標籤的alink屬性
  • bgColor -- 表明HTML body標籤的bgcolor屬性
  • fgColor -- 表明HTML body標籤的text屬性
  • linkColor -- 表明HTML body標籤的link屬性
  • vlinkColor -- 表明HTML body標籤的vlink屬性
  • lastModified -- 頁面最後修改的日期字符串,能夠使用Date的構造函數轉換爲日期,例如:new Date(document.lastModified);
  • referrer -- 瀏覽器history中後退一個位置的URL

因爲document表明HTML文檔的內容,所以能夠經過它表示文檔中加載的一些元素,這些元素所有經過集合訪問。html

  • anchors -- 文檔中全部錨(a name="aname")的集合
  • applets -- 文檔中全部applet標籤表示的內容的集合
  • embeds -- 文檔中全部embed標籤表示的內容的集合
  • forms -- 文檔中全部form標籤表示的內容的集合
  • images -- 文檔中全部image標籤表示的內容的集合
  • links -- 文檔中全部a(連接)標籤表示的內容的集合

document函數

  • JavaScript write(str) 函數:在文檔中寫入字符串
  • JavaScript writeln(str) 函數:在文檔中寫入字符串,並在字符串的末尾增長一個換行符
  • JavaScript document.open() 函數:打開已經載入的文檔
  • JavaScript document.close() 函數:用於關閉document.open方法打開的文檔

使用document索引頁面內的元素

能夠使用數字或名稱索引頁面中的元素集合,每一個元素的屬性都變成了集合中相應對象的屬性。java

示例

<form name="form1"><a href="http://www.dreamdu.com/xhtml/" name="a1">xhtml</a></form>
<form name="form2"><a href="http://www.dreamdu.com/css/" name="a2">css</a></form>
<form name="form3"><a href="http://www.dreamdu.com/javascript/" name="a3">javascript</a></form>

<input type="button" value="顯示第二個表單的名稱" onclick="alert(document.forms[1].name)" />
<input type="button" value="顯示第二個表單的名稱第二種方法" onclick="alert(document.forms['form2'].name)" />
<input type="button" value="顯示第三個連接的名稱" onclick="alert(document.links[2].name)" />
<input type="button" value="顯示第三個連接的名稱第二種方法" onclick="alert(document.links['a3'].name)" />
<input type="button" value="顯示第三個連接href屬性的值" onclick="alert(document.links[2].href)" />

表示第二個表單的方法:document.forms[1]或document.forms["form2"]瀏覽器

表示第三個連接的方法:document.links[2]或document.links["a3"]cookie

表示第三個連接href屬性的方法:document.links[2].hrefapp

相關文章
相關標籤/搜索