亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

JavaScript HTML DOM elements (nodes)

Create HTML elements

var newEle =document.createElement(p);

The created element is appended to other elements:

A. appendChild(B): If B is a newly created element, add element B to the end of all child elements of the A element.

If the element already exists in page B, the effect of this statement is to move the element B to the child element of A.

appendChild() This function has similar effects to the innerHTML attribute. The difference is:

1 innerHTML will run slower than appendChild (maybe because it needs to be parsed)

2 innerHTML is more convenient than appendChild, just like writing a string

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文網(wǎng)(php.cn)</title>
</head>
<body>
<div id="div1">
<p id="p1">這是一個(gè)段落。</p>
<p id="p2">這是另一個(gè)段落。</p>
</div>
<script>
var para=document.createElement("p");
var node=document.createTextNode("這是一個(gè)新段落。");
para.appendChild(node);
var element=document.getElementById("div1");
element.appendChild(para);
</script>
</body>
</html>

Example analysis:

This code creates a new

element:

var para=document.createElement("p");

To add text to a

element, you must first create a text node. This code creates a text node:

var node=document.createTextNode("This is a new paragraph.");

Then you have to

Element appends this text node:

para.appendChild(node);

Finally you must append this new element to an existing element.

This code finds an existing element:

var element=document.getElementById("div1");

The following code is already Add new elements after existing elements:

element.appendChild(para);


Delete HTML elements

The removechild function can delete the specified child elements of the parent element.

If this function successfully deletes the child node, it returns the deleted node, otherwise it returns null.

Syntax structure:

fatherObj.removeChild(childrenObj)

Parameter explanation:

1.fatherObj: The element object of the child element to be deleted.
2.childrenObj: The child element object to be deleted.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>php中文網(wǎng)(php.cn)</title>
</head>
<body>
<div id="div1">
<p id="p1">這是一個(gè)段落。</p>
<p id="p2">這是另一個(gè)段落。</p>
</div>
<script>
var parent=document.getElementById("div1");
var child=document.getElementById("p1");
parent.removeChild(child);
</script>
</body>
</html>

Example analysis

This HTML document contains a <div> element with two child nodes (two <p> elements):

<div id="div1">
<p id="p1">這是一個(gè)段落。</p>
<p id="p2">這是另一個(gè)段落。</p>
</div>

Find the element with id="div1":

var parent=document.getElementById("div1");

Find the element with id="p1"

Element:

var child=document.getElementById("p1");

Remove child element from parent element:

parent.removeChild(child);
Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <script> //顯示用戶信息 function ShowData() { var userList = GetUserList(); var tabUser = document.getElementById("tabUser"); for (var i = 0; i < userList.length; i++) { var tr = document.createElement("tr"); var td1 = document.createElement("td"); var td2 = document.createElement("td"); td1.innerHTML = userList[i].UserId; td2.innerHTML = userList[i].UserName; tr.appendChild(td1); tr.appendChild(td2); tabUser.appendChild(tr); } } //清空用戶信息 function RemoveData() { var tabUser = document.getElementById("tabUser"); for (var i = tabUser.childNodes.length - 1; i >= 0; i--) { tabUser.removeChild(tabUser.childNodes[i]); } } //獲取用戶列表 function GetUserList() { var userList = []; var user1 = { UserId: 1, UserName: "Kevin" }; var user2 = { UserId: 2, UserName: "Joins" }; userList.push(user1); userList.push(user2); return userList; } </script> </head> <body> <table id="tabUser" border="1"></table> <input type="button" id="btnShowData" onclick="ShowData()" value="顯示數(shù)據(jù)" /> <input type="button" id="btnRemoveData" onclick="RemoveData()" value="清空數(shù)據(jù)" /> </body> </html>
submitReset Code