JavaScript 與html的元素產生關聯
前情摘要
一個需求。
存在一個長寬為50畫素的淡灰色div塊,想讓它在被點選後,文字變成“White”,背景顏色變成深沉的黑。如何使用JavaScript來實現呢?
效果
點選前
點選後
關鍵字
- getElementById
- 通過id獲取網頁中的元素
- getElementsByClassName
- 通過類名獲取網頁中的元素,會獲得一個陣列
- getElementsByTagName
- 通過標籤獲取網頁中的元素,會獲得一個陣列
Example
getElementById
首先,在html檔案中準備一個div備用
<!--樣式-->
#div_text {
width:50px;
height:50px;
background-color:#CCC;
}
<!--待會要用到的div-->
<div id="div_text" onclick="changeColorAndFont()">Black</div>
參考解釋:
- 定義了一個id為“div_text”的div標籤
- onclick="changeColorAndFont()"表示當這個div被單擊時,執行changeColorAndFont()函式
接著,我們要寫出changeColorAndFont這個函式,實現我們的需求,更換文字、文字的顏色和背景顏色
function changeColorAndFont() {
var divText = document.getElementById("div_text");
divText.style.backgroundColor = "#333";
divText.style.color = "#FFF";
divText.firstChild.nodeValue = "White";
}
參考解釋:
var divText = document.getElementById("div_text");
利用document.getElementById,引數是字串,獲取id為”div_text”的元素,並將該元素賦給divText。
divText.style.backgroundColor = "#333";
divText.style.color = "#FFF";
通過.style獲取該元素的CSS屬性,其實看名字也可以看出來backgroundColor是背景顏色,color是字型顏色。
divText.firstChild.nodeValue = "White";
這裡的firstChild可能一開始比較難理解
<body>
<h1></h1>
<div>Black</div>
</body>
在html中,有節點這麼一個概念,比如上面的程式碼,劃分一下,就是
- body
- h1
- div
- Black
像族譜一樣,body的子節點(child)有兩個,分別是h1和div。
然而值得注意的是,Black這個文字內容,也算是div的一個子節點(文字節點)。
所以divText.firstChild.value的意思就是其後代文字節點的值,如果在改變其的值之前使用
alert(divText.firstChild.value);
輸出,那麼將會輸出”Black”。
divText.firstChild.nodeValue = "White";
而這句的意思就很清楚了,將其第一個後代的節點值改變為White。
完整程式碼
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>getElementById</title>
<style type="text/css">
#div_text {
width:50px;
height:50px;
background-color:#CCC;
}
</style>
<script type="text/javascript">
function changeColorAndFont() {
var divText = document.getElementById("div_text");
//alert(divText.firstChild.nodeValue);
divText.style.backgroundColor = "#333";
divText.style.color = "#FFF";
divText.firstChild.nodeValue = "White";
}
</script>
</head>
<body>
<div id="div_text" onclick="changeColorAndFont()">Black</div>
</body>
</html>
getElementsByTagName
用法和getElementById的很相似,不過其多出的字母s,道出了兩個方法的差異。
假設html程式碼中存在2個div
<body>
<div id="div_text1" onclick="changeColorAndFont()">Black</div>
<div id="div_text2" onclick="changeColorAndFont()">White</div>
</body>
通過getElementsByTagName的獲取
var divTexts = document.getElementsByTagName("div");
參考解釋:
獲取html中所有的div元素
那麼divTexts將是一個陣列,利用item是獲取其子節點。
alert("第一個div是:" + divTexts.item(0).firstChild.nodeValue + "\n第二個div是:" + divTexts.item(1).firstChild.nodeValue);
參考解釋:
divTexts.item(0).firstChild.nodeValue表示第一個div的第一個子節點的文字值,即Black。
item(1)自然就是表示獲取到第二個div標籤了。
結果
完整程式碼
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>getElementById</title>
<script type="text/javascript">
function changeColorAndFont() {
var divTexts = document.getElementsByTagName("div");
alert("第一個div是:" + divTexts.item(0).firstChild.nodeValue + "\n第二個div是:" + divTexts.item(1).firstChild.nodeValue);
}
</script>
</head>
<body>
<div onclick="changeColorAndFont()">Black</div>
<div onclick="changeColorAndFont()">White</div>
</body>
</html>
getElementsByClassName
通過class的值來獲取元素,備用div
<div class="div_text" onclick="changeColorAndFont()">Black</div>
<div class="div_text" onclick="changeColorAndFont()">White</div>
獲取元素
var divTexts = document.getElementsByClassName("div_text");
參考解釋:
divTexts同樣是一個陣列,通過item訪問元素
alert("第一個div是:" + divTexts.item(0).firstChild.nodeValue + "\n第二個div是:" + divTexts.item(1).firstChild.nodeValue);
可以看到,甚至程式碼都不用換。
結果
完整程式碼
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>getElementById</title>
<style type="text/css">
.div_text {
width:50px;
height:50px;
background-color:#CCC;
}
</style>
<script type="text/javascript">
function changeColorAndFont() {
var divTexts = document.getElementsByClassName("div_text");
alert("第一個div是:" + divTexts.item(0).firstChild.nodeValue + "\n第二個div是:" + divTexts.item(1).firstChild.nodeValue);
}
</script>
</head>
<body>
<div class="div_text" onclick="changeColorAndFont()">Black</div>
<div class="div_text" onclick="changeColorAndFont()">White</div>
</body>
</html>
再看一遍關鍵字有什麼關係呢?
關鍵字
- getElementById
- 通過id獲取網頁中的元素
- getElementsByClassName
- 通過類名獲取網頁中的元素,會獲得一個陣列
- getElementsByTagName
- 通過標籤獲取網頁中的元素,會獲得一個陣列
END
相關文章
- 【前端】HTML__內聯元素與塊元素前端HTML
- HTML 塊級元素和內聯元素HTML
- HTML的行內元素與塊級元素的區別?HTML
- 淺析HTML、CSS、JavaScript之間的聯絡與區別!HTMLCSSJavaScript
- HTML 替換元素與非替換元素HTML
- JavaScript動態新增或者刪除HTML元素JavaScriptHTML
- 關於idea開發遇到javascript動態新增html元素時中文亂碼的問題IdeaJavaScriptHTML
- 內聯元素與流
- ERP與精益生產之間的關係
- HTML————3、HTML元素HTML
- js堅持不懈之13:JavaScript查詢HTML元素的方法JSJavaScriptHTML
- HTML元素HTML
- HTML 元素HTML
- 塊級元素與內聯元素相互轉換
- H5-13 塊元素與行內元素(內聯元素)H5
- 如果列表元素li的兄弟元素為div,會產生什麼情況?
- HTML form 元素HTMLORM
- HTML fieldset元素HTML
- HTML <p> 元素HTML
- HTML p元素HTML
- HTML <img> 元素HTML
- HTML <dialog> 元素HTML
- dblink的關聯與本地關聯差異
- HTML 元素的預設值HTML
- JavaScript 動態新增與刪除元素JavaScript
- JavaScript萬物產生順序JavaScript
- HTML 空元素 And 可替換元素HTML
- HTML——② HTML 元素、屬性詳解HTML
- HTML input 元素概述HTML
- html塊級元素HTML
- HTML常用元素的預設值HTML
- 塊狀元素、內聯元素和內聯塊狀元素
- Java多執行緒——消費者與生產者的關係Java執行緒
- 精益生產與智慧製造的基本關係是什麼
- JavaScript 元素集合JavaScript
- 基礎篇——html與php聯動HTMLPHP
- HTML元素拖動JSHTMLJS
- HTML----元素層級HTML
- html元素,屬性修改HTML