刪除指定節(jié)點,如為空字符則自動刪除下一節(jié)點的函數(shù)需要點擊兩下才能刪除下一節(jié)點,請問問題在哪?
額。。。在最下面。。。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標題文檔</title>
</head>
<body>
<div id="content">
? <h1>html</h1>
? <h1>php</h1>
? <h1>javascript</h1>
? <h1>jquery</h1>
? <h1>java</h1>
</div>
<script type="text/javascript">
//First Method
/*function clearText1() {
? var content=document.getElementById("content");
? content.removeChild(content.childNodes[5]);*/ // 在此完成該函數(shù)
??
? //Second Method
??
? /*function clearText2(){
? ? ? var content=document.getElementById("content");
? var A=content.childNodes[5];
? content.removeChild(A);*/
??
? //Compelet Remove Object
??
? /*function compeletRemoveText(){
? ? ? var content=document.getElementById("content");
? ? ? var A=content.childNodes[5];
? ? ? A=null;
? ? ? content.removeChild(A);*/
? ? ??
?//Remove All Object
?
?/*function?RemoveAllText()?{ ?
?var?content=document.getElementById("content");
??nodel=content.childNodes.length
??for(i=0;i<nodel;i++)??{ ?
??????var?x=content.removeChild(content.childNodes[0]);??}
???}*/
? ? ??
? ? ? //Remove Selected Object
? ? ??
? ? ? function RemoveSelectedText(){
? ? ? ? ? var content=document.getElementById("content");
? ? ? ? ? var i=document.getElementById("select").value-1;
? ? ? ? ? if(content.childNodes[i]!=1){
? ? ? ? ? ? ? content.removeChild(content.childNodes[i].nextSibling);
? ? ? ? ? }
? ? ? ? ? else{
? ? ? ? ? ? ? content.removeChild(content.childNodes[i]);
? ? ? ? ? }
}
</script>
<input type="text" id="select">
<button onclick="RemoveSelectedText()">清除節(jié)點內容</button>
</body>
</html>
比如輸入5,需要點擊兩下,才能刪除“jquery”這個文本,求大神指教
2016-04-11
content.removeChild(content.childNodes[i].nextSibling); ?里還少了個 ?.nextSibling,解決了~