how2j.cn

关键字 简介 示例代码
appendChild
追加节点
示例代码
insertBefore
在前方插入节点
示例代码
练习-动态加载js
示例代码
答案-动态加载js
示例代码
示例 1 : 追加节点   
示例 2 : 在前方插入节点   
示例 3 : 练习-动态加载js   
示例 4 : 答案-动态加载js   

示例 1 :

追加节点

通过appendChild追加节点。 追加节点一定是把新的节点插在最后面
1. 创建新节点
2. 获取父节点
3. 通过appendChild追加
"); window.frames["iframe_show961"].document.write(decodeHtml($("textarea#stepcodeTextarea961").val())); window.frames["iframe_show961"].document.close(); $(window.frames["iframe_show961"]).load(function(){ $("#iframe_show961").height($("#iframe_show961").contents().find("body").height()+showittryitheight); }); $("#iframe_show961").height($("#iframe_show961").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<div id="parentDiv"> <div id="d1">第一个div</div> <div id="d2">第二个div</div> <div id="d3">第三个div</div> </div> <script> function appendDiv(){ var d4= document.createElement("div"); var text = document.createTextNode("第四个div"); d4.appendChild(text); var parentDiv = document.getElementById("parentDiv"); parentDiv.appendChild(d4); } </script> <button onclick="appendDiv()">追加第4个div</button>
"); window.frames["iframe961"].document.write(decodeHtml(code961)); window.frames["iframe961"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe961"]).load(function(){ $("#iframe961").height($("#iframe961").contents().find("body").height()+showittryitheight); }); $("#iframe961").height($("#iframe961").contents().find("body").height()+showittryitheight); alreadyWriteCode961 = code961; $("#rendering961").hide(); $("#rendered961").show(); } var tRereshRetry2DemoPanel961 = setInterval(rereshRetry2DemoPanel961,1000); var binded961 = false; $("textarea#stepcodeTextarea961").keyup(function(){ if(!binded961){ $(window).bind('beforeunload',function(){ binded961 = true; return "xxxx"; }); } var newCode = $(this).val() code961 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code961!=newCode){ // window.frames["iframe961"].document.write("
"); // window.frames["iframe961"].document.write(decodeHtml($("textarea#stepcodeTextarea961").val())); // window.frames["iframe961"].document.close(); // $(window.frames["iframe961"]).load(function(){ // $("#iframe961").height($("#iframe961").contents().find("body").height()+showittryitheight); // }); // code961 = newCode; // } }); $(".tryButton961").click(function(){ $("#tryDiv961").show(); $("#stepcodeTextarea961").focus(); $("#stepcodeTextarea961").height(200); $("#iframe961").height(0); window.frames["iframe961"].document.write("
"); window.frames["iframe961"].document.write(decodeHtml($("textarea#stepcodeTextarea961").val())); window.frames["iframe961"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe961"]).load(function(){ $("#iframe961").height($("#iframe961").contents().find("body").height()+showittryitheight); }); $("#iframe961").height($("#iframe961").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor961.focus(); editor961.setSize(null, "250"); $("#rendering961").hide(); $("#rendered961").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 editor961 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea961"), { 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); } } }); editor961.on("change",function(doc){ if(!binded961){ $(window).bind('beforeunload',function(){ binded961 = true; return "xxxx"; }); } var newCode = doc.getValue(); code961 = newCode; $("textarea#stepcodeTextarea961").val(newCode); if(alreadyWriteCode961!=code961){ lastModifedTime961 = new Date().getTime(); $("#rendering961").show(); $("#rendered961").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor961 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor961.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv961").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

在前方插入节点

有时候,需要在指定位置插入节点,而不是只是追加在后面
这个时候就需要用到insertBefore
1. 创建新节点
2. 获取父节点
3. 获取需要加入的子节点
4. 通过insertBefore插入
注: insertBefore的第一个参数是新元素,第二个参数是插入位置
"); window.frames["iframe_show962"].document.write(decodeHtml($("textarea#stepcodeTextarea962").val())); window.frames["iframe_show962"].document.close(); $(window.frames["iframe_show962"]).load(function(){ $("#iframe_show962").height($("#iframe_show962").contents().find("body").height()+showittryitheight); }); $("#iframe_show962").height($("#iframe_show962").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<div id="parentDiv"> <div id="d1">第一个div</div> <div id="d2">第二个div</div> <div id="d3">第三个div</div> </div> <script> function insertDiv(){ var d25= document.createElement("div"); var text = document.createTextNode("第二点五个div"); d25.appendChild(text); var parentDiv = document.getElementById("parentDiv"); var d3 = document.getElementById("d3"); parentDiv.insertBefore(d25,d3); } </script> <button onclick="insertDiv()">在第二和第三之间,插入第二点五个div</button>
"); window.frames["iframe962"].document.write(decodeHtml(code962)); window.frames["iframe962"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe962"]).load(function(){ $("#iframe962").height($("#iframe962").contents().find("body").height()+showittryitheight); }); $("#iframe962").height($("#iframe962").contents().find("body").height()+showittryitheight); alreadyWriteCode962 = code962; $("#rendering962").hide(); $("#rendered962").show(); } var tRereshRetry2DemoPanel962 = setInterval(rereshRetry2DemoPanel962,1000); var binded962 = false; $("textarea#stepcodeTextarea962").keyup(function(){ if(!binded962){ $(window).bind('beforeunload',function(){ binded962 = true; return "xxxx"; }); } var newCode = $(this).val() code962 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code962!=newCode){ // window.frames["iframe962"].document.write("
"); // window.frames["iframe962"].document.write(decodeHtml($("textarea#stepcodeTextarea962").val())); // window.frames["iframe962"].document.close(); // $(window.frames["iframe962"]).load(function(){ // $("#iframe962").height($("#iframe962").contents().find("body").height()+showittryitheight); // }); // code962 = newCode; // } }); $(".tryButton962").click(function(){ $("#tryDiv962").show(); $("#stepcodeTextarea962").focus(); $("#stepcodeTextarea962").height(200); $("#iframe962").height(0); window.frames["iframe962"].document.write("
"); window.frames["iframe962"].document.write(decodeHtml($("textarea#stepcodeTextarea962").val())); window.frames["iframe962"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe962"]).load(function(){ $("#iframe962").height($("#iframe962").contents().find("body").height()+showittryitheight); }); $("#iframe962").height($("#iframe962").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor962.focus(); editor962.setSize(null, "250"); $("#rendering962").hide(); $("#rendered962").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 editor962 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea962"), { 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); } } }); editor962.on("change",function(doc){ if(!binded962){ $(window).bind('beforeunload',function(){ binded962 = true; return "xxxx"; }); } var newCode = doc.getValue(); code962 = newCode; $("textarea#stepcodeTextarea962").val(newCode); if(alreadyWriteCode962!=code962){ lastModifedTime962 = new Date().getTime(); $("#rendering962").show(); $("#rendered962").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor962 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor962.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv962").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

练习-动态加载js

练习难度
Or  姿势不对,事倍功半! 点击查看做练习的正确姿势
传统的加载js文件的办法是这样

<script src="/study/hello.js"></script>


准备一个test3041.js,其内容是:

alert("this is test3041.js");


做一个按钮,当页面打开的时候,并不会加载这个test3041.js,只有在点击了按钮之后,才加载test3041.js
示例 4 :

答案-动态加载js

在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多


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


关于 前端部分-HTML DOM-插入节点 的提问

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

上传截图