国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

HTML DOM Chinese Reference Manual / HTML DOM 導(dǎo)航

HTML DOM 導(dǎo)航


通過(guò) HTML DOM,您能夠使用節(jié)點(diǎn)關(guān)系在節(jié)點(diǎn)樹(shù)中導(dǎo)航。


HTML DOM 節(jié)點(diǎn)列表

getElementsByTagName() 方法返回節(jié)點(diǎn)列表。節(jié)點(diǎn)列表是一個(gè)節(jié)點(diǎn)數(shù)組。

下面的代碼選取文檔中的所有 <p> 節(jié)點(diǎn):

實(shí)例

<html><!DOCTYPE html>
<html>
<body>

<p>Hello World!</p>
<p>The DOM is very useful!</p>

<script>
x=document.getElementsByTagName("p");
document.write("The innerHTML of the second paragraph is: " + x[1].innerHTML);
</script>

</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

注意:

下標(biāo)號(hào)從 0 開(kāi)始。


HTML DOM 節(jié)點(diǎn)列表長(zhǎng)度

length 屬性定義節(jié)點(diǎn)列表中節(jié)點(diǎn)的數(shù)量。

您可以使用 length 屬性來(lái)循環(huán)節(jié)點(diǎn)列表:

實(shí)例

<html><!DOCTYPE html>
<html>
<body>

<p>Hello World!</p>
<p>The DOM is very useful!</p>
<p>This example demonstrates the <b>length</b> property.</p>

<script>
x=document.getElementsByTagName("p");
for (i=0;i<x.length;i++)
  { 
  document.write(x[i].innerHTML);
  document.write("<br>");
  }
</script>
</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

實(shí)例解析:

  • 獲取所有 <p> 元素節(jié)點(diǎn)

  • 輸出每個(gè) <p> 元素的文本節(jié)點(diǎn)的值


導(dǎo)航節(jié)點(diǎn)關(guān)系

您能夠使用三個(gè)節(jié)點(diǎn)屬性:parentNode、firstChild 以及 lastChild ,在文檔結(jié)構(gòu)中進(jìn)行導(dǎo)航。

請(qǐng)看下面的 HTML 片段:

<html>
<body>

<p>Hello World!</p>
<div>
  <p>The DOM is very useful!</p>
  <p>This example demonstrates node relationships.</p>
</div>

</body>
</html>
  • 首個(gè) <p> 元素是 <body> 元素的首個(gè)子元素(firstChild)

  • <div> 元素是 <body> 元素的最后一個(gè)子元素(lastChild)

  • <body> 元素是首個(gè) <p> 元素和 <div> 元素的父節(jié)點(diǎn)(parentNode)

firstChild 屬性可用于訪問(wèn)元素的文本:

實(shí)例

<html><!DOCTYPE html>
<html>
<body>

<p id="intro">Hello World!</p>

<script>
x=document.getElementById("intro");
document.write(x.firstChild.nodeValue);
</script>

</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例


DOM 根節(jié)點(diǎn)

這里有兩個(gè)特殊的屬性,可以訪問(wèn)全部文檔:

  • document.documentElement - 全部文檔

  • document.body - 文檔的主體

實(shí)例

<html><!DOCTYPE html>
<html>
<body>

<p>Hello World!</p>
<div>
<p>The DOM is very useful!</p>
<p>This example demonstrates the <b>document.body</b> property.</p>
</div>

<script>
alert(document.body.innerHTML);
</script>

</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例


childNodes 和 nodeValue

除了 innerHTML 屬性,您也可以使用 childNodes 和 nodeValue 屬性來(lái)獲取元素的內(nèi)容。

下面的代碼獲取 id="intro" 的 <p> 元素的值:

實(shí)例

<html><!DOCTYPE html>
<html>
<body>

<p id="intro">Hello World!</p>

<script>
txt=document.getElementById("intro").childNodes[0].nodeValue;
document.write(txt);
</script>

</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

在上面的例子中,getElementById 是一個(gè)方法,而 childNodes 和 nodeValue 是屬性。

在本教程中,我們將使用 innerHTML 屬性。不過(guò),學(xué)習(xí)上面的方法有助于對(duì) DOM 樹(shù)結(jié)構(gòu)和導(dǎo)航的理解。