All HTML elements are objects. And as we know every object has properties and methods. The document object has methods that allow you to select the desired HTML element. These three methods are the most commonly used for selecting HTML elements:
The getElementsByClassName() method returns a collection of all elements in the document with the specified class name. For example, if our HTML page contained three elements with class="demo", the following code would return all those elements as an array:
var arr = document.getElementsByClassName("demo"); //accessing the second element arr[1].innerHTML = "Hi";
Similarly, the getElementsByTagName method returns all of the elements of the specified tag name as an array. The following example gets all paragraph elements of the page and changes their content:
<p>hi</p> <p>hello</p> <p>hi</p> <script> var arr = document.getElementsByTagName("p"); for (var x = 0; x < arr.length; x++) { arr[x].innerHTML = "Hi there"; } </script>
The script will result in the following HTML:
腳本(script)會讓結果變成以下的HTML:
<p>Hi there</p> <p>Hi there</p> <p>Hi there</p>
TIP:We used the length property of the array to loop through all the selected elements in the above example.
提示:在上方的範例中,我們使用陣列的length屬性去所有選取的元件中跑迴圈。
-----
Working with DOM
操作DOM
Each element in the DOM has a set of properties and methods that provide information about their relationships in the DOM: element.childNodes returns an array of an element's child nodes. element.firstChild returns the first child node of an element. element.lastChild returns the last child node of an element. element.hasChildNodes returns true if an element has any child nodes, otherwise false. element.nextSibling returns the next node at the same tree level. element.previousSibling returns the previous node at the same tree level. element.parentNode returns the parent node of an element.
We can, for example, select all child nodes of an element and change their content:
<html> <body> <div id ="demo"> <p>some text</p> <p>some other text</p> </div>
<script> var a = document.getElementById("demo"); var arr = a.childNodes; for(var x=0;x<arr.length;x++) { arr[x].innerHTML = "new text"; } </script>
</body> </html>
TIP:The code above changes the text of both paragraphs to "new text".
提示:上面的程式碼將兩個段落的內容都改成"new text"。
-----
Changing Attributes
改變屬性
Once you have selected the element(s) you want to work with, you can change their attributes. As we have seen in the previous lessons, we can change the text content of an element using the innerHTML property. Similarly, we can change the attributes of elements. For example, we can change the src attribute of an image:
The style of HTML elements can also be changed using JavaScript. All style attributes can be accessed using the style object of the element. For example:
<div id="demo" style="width:200px">some text</div> <script> var x = document.getElementById("demo"); x.style.color = "6600FF"; x.style.width = "100px" </script>
The code above changes the text color and width of the div element.
上面的程式碼改變該元素的內容顏色和寬度。
TIP:All CSS properties can be set and modified using JavaScript. Just remember, that you cannot use dashes (-) in the property names: these are replaced with camelCase versions, where the compound words begin with a capital letter. For example: the background-color property should be referred to as backgroundColor.
Use the following methods to create new nodes: element.cloneNode() clones an element and returns the resulting node. document.createElement(element) creates a new element node. document.createTextNode(text) creates a new text node.
var node = document.createTextNode("Some new text");
This will create a new text node, but it will not appear in the document until you append it to an existing element with one of the following methods: element.appendChild(newNode) adds a new child node to an element as the last child node. element.insertBefore(node1, node2) inserts node1 as a child before node2.
<script> //creating a new paragraph var p = document.createElement("p"); var node = document.createTextNode("Some new text"); //adding the text to the paragraph p.appendChild(node);
var div = document.getElementById("demo"); //adding the paragraph to the div div.appendChild(p); </script>
TIP:This creates a new paragraph and adds it to the existing div element on the page.
提示:這新增一個段落並將它加到網頁現有的div元素中。
-----
翻譯小心得| DOM操作的基礎了解,之前用DOM比較少用來操作class和tag name,幾乎都是取id(因為是單一值),比較不會改到不想改的東西,之後再想想看有什麼情境下可以用class和tag來操作會比較方便。 Finally, the article above is from SOLOLEARN . I translated it just for practice, personal use only. If that is inappropriate, please contact me.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.