欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript動態(tài)改變HTML頁面元素例如添加或刪除

 更新時間:2014年08月10日 16:34:04   投稿:whsnow  
HTML頁面元素可以通過js動態(tài)改變,比如可以向HTML中添加元素或刪除某個元素,下面為示例代碼,感興趣的朋友不要錯過

可以通過JavaScript動態(tài)的改變HTML中的元素

向HTML中添加元素

首先需要創(chuàng)建一個標簽,然后向該標簽中添加相應(yīng)的內(nèi)容,然后將創(chuàng)建好的標簽添加到相應(yīng)的位置。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>測試文檔</title> 
<script type="text/javascript"> 
function add(){ 
var element = document.createElement("p"); 
var node = document.createTextNode("添加新段落"); 
element.appendChild(node); 
x = document.getElementById("demo"); 
x.appendChild(element); 
} 
</script> 
</head> 
<body> 
<div id="demo"> 
<p>這是第一段</p> 
</div> 
<input type="button" value="按鈕" onclick="add()" /> 
</body> 
</html>

刪除HTML中的某個元素

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>測試文檔</title> 
<script type="text/javascript"> 
function deleteE(){ 
var father = document.getElementById("demo"); 
var child = document.getElementById("p1"); 
father.removeChild(child); 
} 
</script> 
</head> 
<body> 
<div id="demo"> 
<p id="p1">這是第一段</p> 
<p id="p2">這是第二段</p> 
</div> 
<input type="button" value="刪除" onclick="deleteE()" /> 
</body> 
</html>

相關(guān)文章

最新評論