本經驗是介紹JavaScript說話中,利用removeChild() 函數刪除節點的詳解。
removeChild() 函數的介紹如下圖:
removeChild() 函數的語法如下圖:
removeChild() 函數語法參數申明:
removeChild() 函數的實現代碼:
//刪除函數
function removeDom(){
var deldom=document.getElementById('removedom');
if(deldom.hasChildNodes()){
deldom.removeChild(deldom.lastChild);
}
}
實現函數的挪用代碼如下:
<body>
<h1>刪除節點</h1>
<div id="removedom">
<p>節點一</p>
<p>節點二</p>
</div>
<form>
<input type="button" value="刪除操作" onclick="removeDom();"/>
</form>
</body>
示例的完整代碼:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus?">
<title>removeChild方式示例</title>
<script language="javascript">
//刪除函數
function removeDom(){
var deldom=document.getElementById('removedom');
if(deldom.hasChildNodes()){
deldom.removeChild(deldom.lastChild);
}
}
</script>
</head>
<body>
<h1>刪除節點</h1>
<div id="removedom">
<p>節點一</p>
<p>節點二</p>
</div>
<form>
<input type="button" value="刪除操作" onclick="removeDom();"/>
</form>
</body>
</html>
示例利用結果:
0 篇文章
如果覺得我的文章對您有用,請隨意打賞。你的支持將鼓勵我繼續創作!