JavaScript 增加和刪除標簽


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>

<style>
#div1{
height: 200px;
background-color: #84a42b;
}
</style>
</head>
<body>

<div id="div1">
<div id="div2">hello div2</div>
<p>hello p</p>
<!--<p><em>hello ppppp</em></p>-->
</div>

<input type="button" value="添加p" onclick="add();">
<input type="button" value="刪除p" onclick="remove();">

<script>
function remove() {
var ele=document.getElementById("div1");

var last_son=ele.lastElementChild;

ele.removeChild(last_son);
}
function add() {
var ele=document.getElementById("div1");
var son=document.createElement("p");
son.innerHTML="<em>hello ppppp</em>"
// son.innerText="<em>hello ppppp</em>";
ele.appendChild(son);


}
</script>
</body>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM