2020国产成人精品视频,性做久久久久久久久,亚洲国产成人久久综合一区,亚洲影院天堂中文av色

分享

HTML DOM文檔對象查找元素的方法

 禁忌石 2019-11-15

        2018-09-03 10:24:21 

        版權(quán)聲明:本文為博主原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接和本聲明。

        本文鏈接:https://blog.csdn.net/wd2011063437/article/details/80987809

        一、HTML DOM 節(jié)點

        在 HTML DOM (Document Object Model) 中 , 每一個元素都是 節(jié)點:

        1. 文檔是一個文檔節(jié)點。

        2. 所有的HTML元素都是元素節(jié)點。

        3. 所有 HTML 屬性都是屬性節(jié)點。

        4. 文本插入到 HTML 元素是文本節(jié)點。

        5. 注釋是注釋節(jié)點。

        二、Document 對象

        當瀏覽器載入 HTML 文檔, 它就會成為 Document 對象。
          Document 對象是 HTML 文檔的根節(jié)點。
          Document 對象使我們可以從腳本中對 HTML 頁面中的所有元素進行訪問。
          提示:Document 對象是 Window 對象的一部分,可通過 window.document 屬性對其進行訪問。

        三、HTML DOM 定義的查找對象的方法

        HTML文檔常見的方法:

        • getElementsByName()

        • getElementsByTagName()

        • getElementsByClassName()

        • getElementById()

        (一)、getElementsByName()

        1、定義和用法
          getElementsByName() 方法可返回帶有指定名稱的對象的集合

        2、語法
          document.getElementsByName(name)

        3、參數(shù)
          name 必須。元素的名稱。

        4、實例

        <!DOCTYPE html><html><head>
        	<meta charset="utf-8">
        	<title>getElementsByName()實例</title>
        	<script>
        		function getElements(){
        			var f=document.getElementsByName("fruit");
        			alert(f.length);
        		}
        	</script></head><body>
        	蘋果:<input name="fruit" type="radio" value="蘋果">
        	香蕉:<input name="fruit" type="radio" value="香蕉">
        	<input type="button" onclick="getElements()" value="name為 'fruit'的元素數(shù)量?"></body></html>
        • 1

        • 2

        • 3

        • 4

        • 5

        • 6

        • 7

        • 8

        • 9

        • 10

        • 11

        • 12

        • 13

        • 14

        • 15

        • 16

        • 17

        • 18

        5、效果圖
        這里寫圖片描述

        (二)、getElementsByTagName()

        1、定義和用法
          getElementsByTagName() 方法可返回帶有指定標簽名的對象的集合
          提示: 參數(shù)值 “*” 返回文檔的所有元素。

        2、語法
          document.getElementsByTagName(tagname)

        3、參數(shù)
          tagname String 必須。你要獲取元素的標簽名。

        4、返回值
          NodeList 對象 指定標簽名的元素集合

        5、實例

        <!DOCTYPE html><html><head>
        	<meta charset="utf-8">
        	<title>getElementsByTagName()實例</title></head><body>
        	<p id="demo">單擊按鈕來改變這一段中的文本。</p>
        	<button onclick="myFunction()">點我</button>
        	<script>
        		function myFunction(){
        			document.getElementsByTagName("P")[0].innerHTML="Hello World";
        		};
        	</script></body></html>
        • 1

        • 2

        • 3

        • 4

        • 5

        • 6

        • 7

        • 8

        • 9

        • 10

        • 11

        • 12

        • 13

        • 14

        • 15

        • 16

        6、效果
         單擊按鈕前:
        這里寫圖片描述

        單擊按鈕后:
        這里寫圖片描述

        (三)、getElementsByClassName()

        1、定義和使用
          getElementsByClassName() 方法返回文檔中所有指定類名的元素集合,作為 NodeList 對象。NodeList 對象代表一個有順序的節(jié)點列表。NodeList 對象 我們可通過節(jié)點列表中的節(jié)點索引號來訪問列表中的節(jié)點(索引號由0開始)。
          提示: 你可以使用 NodeList 對象的 length 屬性來確定指定類名的元素個數(shù),并循環(huán)各個元素來獲取你需要的那個元素。
          注意: Internet Explorer 8 及更早 IE 版本不支持 getElementsByClassName() 方法。

        2、語法
          document.getElementsByClassName(classname)

        3、參數(shù)
          classname String 必須。你需要獲取的元素類名。
          多個類名使用空格分隔,如 “test demo”。

        4、實例

        <!DOCTYPE html><html><head>
        	<meta charset="utf-8">
        	<title>getElementsByClassName()實例</title>
        	<style>
        		div {
        		    border: 1px solid black;
        		    margin: 5px;
        		}
        		.example {
        		    border: 1px solid black;
        		    margin: 5px;
        		}
        	</style></head><body><h1>實例一</h1><div class="nocolor">
        	<p>P 元素在在第一個樣式為 class="example" 的 Div 元素中。Div 的索引值為 0。</p></div><div class="color red">
        	<p>P 元素在在第一個樣式為 class="example color" 的 Div 元素中。Div 的索引值為 0。</p></div><div class="color yellow">
        	<p>P 元素在在第二個樣式為 class="example color" 的 Div 元素中。Div 的索引值為 1。</p></div>
        

        感谢您访问我们的网站,您可能还对以下资源感兴趣:

        2020国产成人精品视频,性做久久久久久久久,亚洲国产成人久久综合一区,亚洲影院天堂中文av色