how2j.cn

关键字 简介 示例代码
createElement
创建元素节点
示例代码
createTextNode
创建文本节点
示例代码
createAttribute
创建属性节点
示例代码
练习-动态创建一个表
示例代码
答案-动态创建一个表
示例代码
示例 1 : 创建元素节点   
示例 2 : 创建文本节点   
示例 3 : 创建属性节点   
示例 4 : 练习-动态创建一个表   
示例 5 : 答案-动态创建一个表   

示例 1 :

创建元素节点

通过createElement 创建一个新的元素节点
接着把该元素节点,通过appendChild加入到另一个元素节点div1中
"); window.frames["iframe_show958"].document.write(decodeHtml($("textarea#stepcodeTextarea958").val())); window.frames["iframe_show958"].document.close(); $(window.frames["iframe_show958"]).load(function(){ $("#iframe_show958").height($("#iframe_show958").contents().find("body").height()+showittryitheight); }); $("#iframe_show958").height($("#iframe_show958").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<html> <div id="d">Hello HTML DOM</div> <script> function add(){ var hr=document.createElement("hr"); var div1 = document.getElementById("d"); div1.appendChild(hr); } </script> <button onclick="add()">在div中追加一个hr元素</button> </html>
<html>
<div id="d">Hello HTML DOM</div>
<script>
function add(){
  var hr=document.createElement("hr");
  var div1 = document.getElementById("d");
  div1.appendChild(hr);
}
</script>

<button onclick="add()">在div中追加一个hr元素</button>

</html>
"); window.frames["iframe958"].document.write(decodeHtml(code958)); window.frames["iframe958"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe958"]).load(function(){ $("#iframe958").height($("#iframe958").contents().find("body").height()+showittryitheight); }); $("#iframe958").height($("#iframe958").contents().find("body").height()+showittryitheight); alreadyWriteCode958 = code958; $("#rendering958").hide(); $("#rendered958").show(); } var tRereshRetry2DemoPanel958 = setInterval(rereshRetry2DemoPanel958,1000); var binded958 = false; $("textarea#stepcodeTextarea958").keyup(function(){ if(!binded958){ $(window).bind('beforeunload',function(){ binded958 = true; return "xxxx"; }); } var newCode = $(this).val() code958 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code958!=newCode){ // window.frames["iframe958"].document.write("
"); // window.frames["iframe958"].document.write(decodeHtml($("textarea#stepcodeTextarea958").val())); // window.frames["iframe958"].document.close(); // $(window.frames["iframe958"]).load(function(){ // $("#iframe958").height($("#iframe958").contents().find("body").height()+showittryitheight); // }); // code958 = newCode; // } }); $(".tryButton958").click(function(){ $("#tryDiv958").show(); $("#stepcodeTextarea958").focus(); $("#stepcodeTextarea958").height(200); $("#iframe958").height(0); window.frames["iframe958"].document.write("
"); window.frames["iframe958"].document.write(decodeHtml($("textarea#stepcodeTextarea958").val())); window.frames["iframe958"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe958"]).load(function(){ $("#iframe958").height($("#iframe958").contents().find("body").height()+showittryitheight); }); $("#iframe958").height($("#iframe958").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor958.focus(); editor958.setSize(null, "250"); $("#rendering958").hide(); $("#rendered958").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 editor958 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea958"), { 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); } } }); editor958.on("change",function(doc){ if(!binded958){ $(window).bind('beforeunload',function(){ binded958 = true; return "xxxx"; }); } var newCode = doc.getValue(); code958 = newCode; $("textarea#stepcodeTextarea958").val(newCode); if(alreadyWriteCode958!=code958){ lastModifedTime958 = new Date().getTime(); $("#rendering958").show(); $("#rendered958").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor958 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor958.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv958").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

创建文本节点

首先创建一个元素节点p (p是p标签,不是随便命名的变量名)
接着通过createTextNode创建一个内容节点text
把text加入到p
再把p加入到div
"); window.frames["iframe_show960"].document.write(decodeHtml($("textarea#stepcodeTextarea960").val())); window.frames["iframe_show960"].document.close(); $(window.frames["iframe_show960"]).load(function(){ $("#iframe_show960").height($("#iframe_show960").contents().find("body").height()+showittryitheight); }); $("#iframe_show960").height($("#iframe_show960").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<html> <div id="d">Hello HTML DOM</div> <script> function add(){ var p=document.createElement("p"); var text = document.createTextNode("这是通过DOM创建出来的<p>"); p.appendChild(text); var div1 = document.getElementById("d"); div1.appendChild(p); } </script> <button onclick="add()">在div中追加一个p元素</button> </html>
<html>
<div id="d">Hello HTML DOM</div>
<script>
function add(){

  var p=document.createElement("p");
  var text = document.createTextNode("这是通过DOM创建出来的<p>");
  p.appendChild(text);

  var div1 = document.getElementById("d");
  div1.appendChild(p);

}
</script>
 
<button onclick="add()">在div中追加一个p元素</button>
 
</html>
"); window.frames["iframe960"].document.write(decodeHtml(code960)); window.frames["iframe960"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe960"]).load(function(){ $("#iframe960").height($("#iframe960").contents().find("body").height()+showittryitheight); }); $("#iframe960").height($("#iframe960").contents().find("body").height()+showittryitheight); alreadyWriteCode960 = code960; $("#rendering960").hide(); $("#rendered960").show(); } var tRereshRetry2DemoPanel960 = setInterval(rereshRetry2DemoPanel960,1000); var binded960 = false; $("textarea#stepcodeTextarea960").keyup(function(){ if(!binded960){ $(window).bind('beforeunload',function(){ binded960 = true; return "xxxx"; }); } var newCode = $(this).val() code960 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code960!=newCode){ // window.frames["iframe960"].document.write("
"); // window.frames["iframe960"].document.write(decodeHtml($("textarea#stepcodeTextarea960").val())); // window.frames["iframe960"].document.close(); // $(window.frames["iframe960"]).load(function(){ // $("#iframe960").height($("#iframe960").contents().find("body").height()+showittryitheight); // }); // code960 = newCode; // } }); $(".tryButton960").click(function(){ $("#tryDiv960").show(); $("#stepcodeTextarea960").focus(); $("#stepcodeTextarea960").height(200); $("#iframe960").height(0); window.frames["iframe960"].document.write("
"); window.frames["iframe960"].document.write(decodeHtml($("textarea#stepcodeTextarea960").val())); window.frames["iframe960"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe960"]).load(function(){ $("#iframe960").height($("#iframe960").contents().find("body").height()+showittryitheight); }); $("#iframe960").height($("#iframe960").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor960.focus(); editor960.setSize(null, "250"); $("#rendering960").hide(); $("#rendered960").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 editor960 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea960"), { 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); } } }); editor960.on("change",function(doc){ if(!binded960){ $(window).bind('beforeunload',function(){ binded960 = true; return "xxxx"; }); } var newCode = doc.getValue(); code960 = newCode; $("textarea#stepcodeTextarea960").val(newCode); if(alreadyWriteCode960!=code960){ lastModifedTime960 = new Date().getTime(); $("#rendering960").show(); $("#rendered960").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor960 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor960.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv960").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

创建属性节点

首先创建一个元素节点a
接着创建一个内容节点content
把content加入到a
然后通过createAttribute创建一个属性节点 href
设置href的值为http:12306.com
通过setAttributeNode把该属性设置到元素节点a上
最后把a加入到div
"); window.frames["iframe_show959"].document.write(decodeHtml($("textarea#stepcodeTextarea959").val())); window.frames["iframe_show959"].document.close(); $(window.frames["iframe_show959"]).load(function(){ $("#iframe_show959").height($("#iframe_show959").contents().find("body").height()+showittryitheight); }); $("#iframe_show959").height($("#iframe_show959").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<html> <div id="d">Hello HTML DOM<br></div> <script> function add(){ var a=document.createElement("a"); var content = document.createTextNode("http://12306.com"); a.appendChild(content); var href = document.createAttribute("href"); href.nodeValue="http://12306.com"; a.setAttributeNode(href); var div1 = document.getElementById("d"); div1.appendChild(a); } </script> <button onclick="add()">在div中追加一个超链</button> </html>
"); window.frames["iframe959"].document.write(decodeHtml(code959)); window.frames["iframe959"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe959"]).load(function(){ $("#iframe959").height($("#iframe959").contents().find("body").height()+showittryitheight); }); $("#iframe959").height($("#iframe959").contents().find("body").height()+showittryitheight); alreadyWriteCode959 = code959; $("#rendering959").hide(); $("#rendered959").show(); } var tRereshRetry2DemoPanel959 = setInterval(rereshRetry2DemoPanel959,1000); var binded959 = false; $("textarea#stepcodeTextarea959").keyup(function(){ if(!binded959){ $(window).bind('beforeunload',function(){ binded959 = true; return "xxxx"; }); } var newCode = $(this).val() code959 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code959!=newCode){ // window.frames["iframe959"].document.write("
"); // window.frames["iframe959"].document.write(decodeHtml($("textarea#stepcodeTextarea959").val())); // window.frames["iframe959"].document.close(); // $(window.frames["iframe959"]).load(function(){ // $("#iframe959").height($("#iframe959").contents().find("body").height()+showittryitheight); // }); // code959 = newCode; // } }); $(".tryButton959").click(function(){ $("#tryDiv959").show(); $("#stepcodeTextarea959").focus(); $("#stepcodeTextarea959").height(200); $("#iframe959").height(0); window.frames["iframe959"].document.write("
"); window.frames["iframe959"].document.write(decodeHtml($("textarea#stepcodeTextarea959").val())); window.frames["iframe959"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe959"]).load(function(){ $("#iframe959").height($("#iframe959").contents().find("body").height()+showittryitheight); }); $("#iframe959").height($("#iframe959").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor959.focus(); editor959.setSize(null, "250"); $("#rendering959").hide(); $("#rendered959").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 editor959 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea959"), { 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); } } }); editor959.on("change",function(doc){ if(!binded959){ $(window).bind('beforeunload',function(){ binded959 = true; return "xxxx"; }); } var newCode = doc.getValue(); code959 = newCode; $("textarea#stepcodeTextarea959").val(newCode); if(alreadyWriteCode959!=code959){ lastModifedTime959 = new Date().getTime(); $("#rendering959").show(); $("#rendered959").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor959 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor959.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv959").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

练习-动态创建一个表

练习难度
Or  姿势不对,事倍功半! 点击查看做练习的正确姿势
练习-表格斑马线中的表格,使用动态的方式创建出来
"); window.frames["iframe_show2941"].document.write(decodeHtml($("textarea#stepcodeTextarea2941").val())); window.frames["iframe_show2941"].document.close(); $(window.frames["iframe_show2941"]).load(function(){ $("#iframe_show2941").height($("#iframe_show2941").contents().find("body").height()+showittryitheight); }); $("#iframe_show2941").height($("#iframe_show2941").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
<style> table{ border-collapse:collapse; width:90%; } tr.odd{ background-color:#f8f8f8; } tr{ border-bottom-style: solid; border-bottom-width: 1px; border-bottom-color: lightgray; height:35px; } td{ width:25%; text-align:center; } </style> <div id ="d"> </div> <script> function createTD(text){ var td=document.createElement("td"); var text= document.createTextNode(text); td.appendChild(text); return td; } function createTR(texts){ var tr=document.createElement("tr"); for(var i=0;i<texts.length;i++){ var td = createTD(texts[i]); tr.appendChild(td); } return tr; } function createTable(rows){ var table=document.createElement("table"); for(var i=0;i<rows.length;i++){ var tr= createTR(rows[i]); table.appendChild(tr); } return table; } var row0= new Array("id","名称","血量","伤害"); var row1= new Array("1","gareen","340","58"); var row2= new Array("2","teemo","320","76"); var row3= new Array("3","annie","380","38"); var row4= new Array("4","deadbrother","400","90"); var rows=new Array(row0,row1,row2,row3,row4); var div = document.getElementById("d"); var table=createTable(rows); div.appendChild(table); </script>
"); window.frames["iframe2941"].document.write(decodeHtml(code2941)); window.frames["iframe2941"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe2941"]).load(function(){ $("#iframe2941").height($("#iframe2941").contents().find("body").height()+showittryitheight); }); $("#iframe2941").height($("#iframe2941").contents().find("body").height()+showittryitheight); alreadyWriteCode2941 = code2941; $("#rendering2941").hide(); $("#rendered2941").show(); } var tRereshRetry2DemoPanel2941 = setInterval(rereshRetry2DemoPanel2941,1000); var binded2941 = false; $("textarea#stepcodeTextarea2941").keyup(function(){ if(!binded2941){ $(window).bind('beforeunload',function(){ binded2941 = true; return "xxxx"; }); } var newCode = $(this).val() code2941 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code2941!=newCode){ // window.frames["iframe2941"].document.write("
"); // window.frames["iframe2941"].document.write(decodeHtml($("textarea#stepcodeTextarea2941").val())); // window.frames["iframe2941"].document.close(); // $(window.frames["iframe2941"]).load(function(){ // $("#iframe2941").height($("#iframe2941").contents().find("body").height()+showittryitheight); // }); // code2941 = newCode; // } }); $(".tryButton2941").click(function(){ $("#tryDiv2941").show(); $("#stepcodeTextarea2941").focus(); $("#stepcodeTextarea2941").height(200); $("#iframe2941").height(0); window.frames["iframe2941"].document.write("
"); window.frames["iframe2941"].document.write(decodeHtml($("textarea#stepcodeTextarea2941").val())); window.frames["iframe2941"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe2941"]).load(function(){ $("#iframe2941").height($("#iframe2941").contents().find("body").height()+showittryitheight); }); $("#iframe2941").height($("#iframe2941").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor2941.focus(); editor2941.setSize(null, "250"); $("#rendering2941").hide(); $("#rendered2941").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 editor2941 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea2941"), { 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); } } }); editor2941.on("change",function(doc){ if(!binded2941){ $(window).bind('beforeunload',function(){ binded2941 = true; return "xxxx"; }); } var newCode = doc.getValue(); code2941 = newCode; $("textarea#stepcodeTextarea2941").val(newCode); if(alreadyWriteCode2941!=code2941){ lastModifedTime2941 = new Date().getTime(); $("#rendering2941").show(); $("#rendered2941").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor2941 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor2941.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv2941").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

答案-动态创建一个表

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


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


关于 前端部分-HTML DOM-创建节点 的提问

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

上传截图