how2j.cn

关键字 简介 示例代码
removeChild
删除元素节点
示例代码
removeAttribute
删除属性节点
示例代码
removeChild
删除文本节点
示例代码
示例 1 : 删除元素节点   
示例 2 : 删除属性节点   
示例 3 : 删除文本节点   

示例 1 :

删除元素节点

要删除某个元素节点有两步
第一:先获取该元素的父节点
第二:通过父节点,调用removeChild 删除该节点
"); window.frames["iframe_show1179"].document.write(decodeHtml($("textarea#stepcodeTextarea1179").val())); window.frames["iframe_show1179"].document.close(); $(window.frames["iframe_show1179"]).load(function(){ $("#iframe_show1179").height($("#iframe_show1179").contents().find("body").height()+showittryitheight); }); $("#iframe_show1179").height($("#iframe_show1179").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> function removeDiv(){ var parentDiv = document.getElementById("parentDiv"); var div2= document.getElementById("div2"); parentDiv.removeChild(div2); } </script> <div id="parentDiv"> <div id="div1">安全的div</div> <div id="div2">即将被删除的div</div> </div> <button onclick="removeDiv()">删除第二个div</button>
<script>
function removeDiv(){
  var parentDiv = document.getElementById("parentDiv");
  var div2= document.getElementById("div2");
  parentDiv.removeChild(div2);
}

</script>

<div id="parentDiv">
  <div id="div1">安全的div</div>
  <div id="div2">即将被删除的div</div>
</div>

<button onclick="removeDiv()">删除第二个div</button>

"); window.frames["iframe1179"].document.write(decodeHtml(code1179)); window.frames["iframe1179"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1179"]).load(function(){ $("#iframe1179").height($("#iframe1179").contents().find("body").height()+showittryitheight); }); $("#iframe1179").height($("#iframe1179").contents().find("body").height()+showittryitheight); alreadyWriteCode1179 = code1179; $("#rendering1179").hide(); $("#rendered1179").show(); } var tRereshRetry2DemoPanel1179 = setInterval(rereshRetry2DemoPanel1179,1000); var binded1179 = false; $("textarea#stepcodeTextarea1179").keyup(function(){ if(!binded1179){ $(window).bind('beforeunload',function(){ binded1179 = true; return "xxxx"; }); } var newCode = $(this).val() code1179 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1179!=newCode){ // window.frames["iframe1179"].document.write("
"); // window.frames["iframe1179"].document.write(decodeHtml($("textarea#stepcodeTextarea1179").val())); // window.frames["iframe1179"].document.close(); // $(window.frames["iframe1179"]).load(function(){ // $("#iframe1179").height($("#iframe1179").contents().find("body").height()+showittryitheight); // }); // code1179 = newCode; // } }); $(".tryButton1179").click(function(){ $("#tryDiv1179").show(); $("#stepcodeTextarea1179").focus(); $("#stepcodeTextarea1179").height(200); $("#iframe1179").height(0); window.frames["iframe1179"].document.write("
"); window.frames["iframe1179"].document.write(decodeHtml($("textarea#stepcodeTextarea1179").val())); window.frames["iframe1179"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1179"]).load(function(){ $("#iframe1179").height($("#iframe1179").contents().find("body").height()+showittryitheight); }); $("#iframe1179").height($("#iframe1179").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1179.focus(); editor1179.setSize(null, "250"); $("#rendering1179").hide(); $("#rendered1179").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1179 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1179"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1179.on("change",function(doc){ if(!binded1179){ $(window).bind('beforeunload',function(){ binded1179 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1179 = newCode; $("textarea#stepcodeTextarea1179").val(newCode); if(alreadyWriteCode1179!=code1179){ lastModifedTime1179 = new Date().getTime(); $("#rendering1179").show(); $("#rendered1179").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1179 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1179.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1179").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 2 :

删除属性节点

要删除某个属性节点有两步
第一:先获取该元素节点
第二:元素节点,调用removeAttribute删除指定属性节点
"); window.frames["iframe_show1180"].document.write(decodeHtml($("textarea#stepcodeTextarea1180").val())); window.frames["iframe_show1180"].document.close(); $(window.frames["iframe_show1180"]).load(function(){ $("#iframe_show1180").height($("#iframe_show1180").contents().find("body").height()+showittryitheight); }); $("#iframe_show1180").height($("#iframe_show1180").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> function removeHref(){ var link= document.getElementById("link"); link.removeAttribute("href"); } </script> <a id="link" href="http://12306.com">http://12306.com</a> <br> <button onclick="removeHref()">删除超链的href属性</button>
<script>
function removeHref(){
  var link= document.getElementById("link");
  link.removeAttribute("href");
}

</script>

<a id="link" href="http://12306.com">http://12306.com</a>

<br>
<button onclick="removeHref()">删除超链的href属性</button>

"); window.frames["iframe1180"].document.write(decodeHtml(code1180)); window.frames["iframe1180"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1180"]).load(function(){ $("#iframe1180").height($("#iframe1180").contents().find("body").height()+showittryitheight); }); $("#iframe1180").height($("#iframe1180").contents().find("body").height()+showittryitheight); alreadyWriteCode1180 = code1180; $("#rendering1180").hide(); $("#rendered1180").show(); } var tRereshRetry2DemoPanel1180 = setInterval(rereshRetry2DemoPanel1180,1000); var binded1180 = false; $("textarea#stepcodeTextarea1180").keyup(function(){ if(!binded1180){ $(window).bind('beforeunload',function(){ binded1180 = true; return "xxxx"; }); } var newCode = $(this).val() code1180 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1180!=newCode){ // window.frames["iframe1180"].document.write("
"); // window.frames["iframe1180"].document.write(decodeHtml($("textarea#stepcodeTextarea1180").val())); // window.frames["iframe1180"].document.close(); // $(window.frames["iframe1180"]).load(function(){ // $("#iframe1180").height($("#iframe1180").contents().find("body").height()+showittryitheight); // }); // code1180 = newCode; // } }); $(".tryButton1180").click(function(){ $("#tryDiv1180").show(); $("#stepcodeTextarea1180").focus(); $("#stepcodeTextarea1180").height(200); $("#iframe1180").height(0); window.frames["iframe1180"].document.write("
"); window.frames["iframe1180"].document.write(decodeHtml($("textarea#stepcodeTextarea1180").val())); window.frames["iframe1180"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1180"]).load(function(){ $("#iframe1180").height($("#iframe1180").contents().find("body").height()+showittryitheight); }); $("#iframe1180").height($("#iframe1180").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1180.focus(); editor1180.setSize(null, "250"); $("#rendering1180").hide(); $("#rendered1180").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1180 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1180"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1180.on("change",function(doc){ if(!binded1180){ $(window).bind('beforeunload',function(){ binded1180 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1180 = newCode; $("textarea#stepcodeTextarea1180").val(newCode); if(alreadyWriteCode1180!=code1180){ lastModifedTime1180 = new Date().getTime(); $("#rendering1180").show(); $("#rendered1180").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1180 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1180.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1180").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 3 :

删除文本节点

删除文本节点
1. 通过childNodes[0] 获取文本节点
注:children()[0] 只能获取第一个子元素节点,不能获取文本节点
2. 通过removeChild删除该文本节点
但是这种方式比较麻烦,一般都是直接通过innerHTML设置为空即可。
注: 通过innerHTML=""的方式,同样会导致文本子节点被删除。
"); window.frames["iframe_show1181"].document.write(decodeHtml($("textarea#stepcodeTextarea1181").val())); window.frames["iframe_show1181"].document.close(); $(window.frames["iframe_show1181"]).load(function(){ $("#iframe_show1181").height($("#iframe_show1181").contents().find("body").height()+showittryitheight); }); $("#iframe_show1181").height($("#iframe_show1181").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> function removeDiv1(){ var parentDiv = document.getElementById("parentDiv"); var textNode = parentDiv.childNodes[0]; parentDiv.removeChild(textNode); } function removeDiv2(){ var parentDiv = document.getElementById("parentDiv"); parentDiv.innerHTML=""; } function recover(){ var parentDiv = document.getElementById("parentDiv"); parentDiv.innerHTML="这里是文本 "; } </script> <style> button{ display:block; } </style> <div id="parentDiv"> 这里是文本 </div> <button onclick="removeDiv1()">通过removechild删除div下的文本节点</button> <button onclick="removeDiv2()">通过innerHTML让内容置空</button> <button onclick="recover()">恢复内容</button>
"); window.frames["iframe1181"].document.write(decodeHtml(code1181)); window.frames["iframe1181"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1181"]).load(function(){ $("#iframe1181").height($("#iframe1181").contents().find("body").height()+showittryitheight); }); $("#iframe1181").height($("#iframe1181").contents().find("body").height()+showittryitheight); alreadyWriteCode1181 = code1181; $("#rendering1181").hide(); $("#rendered1181").show(); } var tRereshRetry2DemoPanel1181 = setInterval(rereshRetry2DemoPanel1181,1000); var binded1181 = false; $("textarea#stepcodeTextarea1181").keyup(function(){ if(!binded1181){ $(window).bind('beforeunload',function(){ binded1181 = true; return "xxxx"; }); } var newCode = $(this).val() code1181 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1181!=newCode){ // window.frames["iframe1181"].document.write("
"); // window.frames["iframe1181"].document.write(decodeHtml($("textarea#stepcodeTextarea1181").val())); // window.frames["iframe1181"].document.close(); // $(window.frames["iframe1181"]).load(function(){ // $("#iframe1181").height($("#iframe1181").contents().find("body").height()+showittryitheight); // }); // code1181 = newCode; // } }); $(".tryButton1181").click(function(){ $("#tryDiv1181").show(); $("#stepcodeTextarea1181").focus(); $("#stepcodeTextarea1181").height(200); $("#iframe1181").height(0); window.frames["iframe1181"].document.write("
"); window.frames["iframe1181"].document.write(decodeHtml($("textarea#stepcodeTextarea1181").val())); window.frames["iframe1181"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1181"]).load(function(){ $("#iframe1181").height($("#iframe1181").contents().find("body").height()+showittryitheight); }); $("#iframe1181").height($("#iframe1181").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1181.focus(); editor1181.setSize(null, "250"); $("#rendering1181").hide(); $("#rendered1181").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1181 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1181"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1181.on("change",function(doc){ if(!binded1181){ $(window).bind('beforeunload',function(){ binded1181 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1181 = newCode; $("textarea#stepcodeTextarea1181").val(newCode); if(alreadyWriteCode1181!=code1181){ lastModifedTime1181 = new Date().getTime(); $("#rendering1181").show(); $("#rendered1181").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1181 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1181.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1181").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


关于 前端部分-HTML DOM-删除节点 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 620943819
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: http://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图