how2j.cn


工具版本兼容问题
元素指的是从开始标签到结束标签之间所有的代码
比如
<p>HelloWord</p>

示例 1 : 完整的HTML   
示例 2 : 不完整的元素   
示例 3 : 有的特殊元素   

示例 1 :

完整的HTML

一个完整的HTML文件,应该至少包含html元素,body元素,以及里面的内容
"); window.frames["iframe_show318"].document.write(decodeHtml($("textarea#stepcodeTextarea318").val())); window.frames["iframe_show318"].document.close(); $(window.frames["iframe_show318"]).load(function(){ $("#iframe_show318").height($("#iframe_show318").contents().find("body").height()+showittryitheight); }); $("#iframe_show318").height($("#iframe_show318").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<html> <body> <h1>标题</h1> </body> </html>
<html>

 <body>

   <h1>标题</h1>  
 
 </body>

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


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

不完整的元素

实际开发中,我们即使一个元素不完整,也是可以正常显示的
但这不是因为你写的牛B,是因为浏览器很强大,浏览器能够渲染你写的“错误的代码” 不过,未来不一定能正常显示。
所以尽量把元素写的完整,即有开始标签也有结束标签
"); window.frames["iframe_show319"].document.write(decodeHtml($("textarea#stepcodeTextarea319").val())); window.frames["iframe_show319"].document.close(); $(window.frames["iframe_show319"]).load(function(){ $("#iframe_show319").height($("#iframe_show319").contents().find("body").height()+showittryitheight); }); $("#iframe_show319").height($("#iframe_show319").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<h1> 标题标签没有结束
<h1> 标题标签没有结束
"); window.frames["iframe319"].document.write(decodeHtml(code319)); window.frames["iframe319"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe319"]).load(function(){ $("#iframe319").height($("#iframe319").contents().find("body").height()+showittryitheight); }); $("#iframe319").height($("#iframe319").contents().find("body").height()+showittryitheight); alreadyWriteCode319 = code319; $("#rendering319").hide(); $("#rendered319").show(); } var tRereshRetry2DemoPanel319 = setInterval(rereshRetry2DemoPanel319,1000); var binded319 = false; $("textarea#stepcodeTextarea319").keyup(function(){ if(!binded319){ $(window).bind('beforeunload',function(){ binded319 = true; return "xxxx"; }); } var newCode = $(this).val() code319 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code319!=newCode){ // window.frames["iframe319"].document.write("
"); // window.frames["iframe319"].document.write(decodeHtml($("textarea#stepcodeTextarea319").val())); // window.frames["iframe319"].document.close(); // $(window.frames["iframe319"]).load(function(){ // $("#iframe319").height($("#iframe319").contents().find("body").height()+showittryitheight); // }); // code319 = newCode; // } }); $(".tryButton319").click(function(){ $("#tryDiv319").show(); $("#stepcodeTextarea319").focus(); $("#stepcodeTextarea319").height(200); $("#iframe319").height(0); window.frames["iframe319"].document.write("
"); window.frames["iframe319"].document.write(decodeHtml($("textarea#stepcodeTextarea319").val())); window.frames["iframe319"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe319"]).load(function(){ $("#iframe319").height($("#iframe319").contents().find("body").height()+showittryitheight); }); $("#iframe319").height($("#iframe319").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor319.focus(); editor319.setSize(null, "250"); $("#rendering319").hide(); $("#rendered319").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 editor319 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea319"), { 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); } } }); editor319.on("change",function(doc){ if(!binded319){ $(window).bind('beforeunload',function(){ binded319 = true; return "xxxx"; }); } var newCode = doc.getValue(); code319 = newCode; $("textarea#stepcodeTextarea319").val(newCode); if(alreadyWriteCode319!=code319){ lastModifedTime319 = new Date().getTime(); $("#rendering319").show(); $("#rendered319").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor319 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor319.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv319").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

有的特殊元素

有的特殊元素,没有内容,即开始和结束标签直接放在一起
比如BR HR
"); window.frames["iframe_show320"].document.write(decodeHtml($("textarea#stepcodeTextarea320").val())); window.frames["iframe_show320"].document.close(); $(window.frames["iframe_show320"]).load(function(){ $("#iframe_show320").height($("#iframe_show320").contents().find("body").height()+showittryitheight); }); $("#iframe_show320").height($("#iframe_show320").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<h1>标题1</h1> <br/> <br/> <br/> <h1>标题2前面有3个换行</h1> <p>这是一个水平线,有渲染效果,但是没有文本内容</p> <hr/>
<h1>标题1</h1>
<br/>
<br/>
<br/>
<h1>标题2前面有3个换行</h1>

<p>这是一个水平线,有渲染效果,但是没有文本内容</p>
<hr/>
"); window.frames["iframe320"].document.write(decodeHtml(code320)); window.frames["iframe320"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe320"]).load(function(){ $("#iframe320").height($("#iframe320").contents().find("body").height()+showittryitheight); }); $("#iframe320").height($("#iframe320").contents().find("body").height()+showittryitheight); alreadyWriteCode320 = code320; $("#rendering320").hide(); $("#rendered320").show(); } var tRereshRetry2DemoPanel320 = setInterval(rereshRetry2DemoPanel320,1000); var binded320 = false; $("textarea#stepcodeTextarea320").keyup(function(){ if(!binded320){ $(window).bind('beforeunload',function(){ binded320 = true; return "xxxx"; }); } var newCode = $(this).val() code320 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code320!=newCode){ // window.frames["iframe320"].document.write("
"); // window.frames["iframe320"].document.write(decodeHtml($("textarea#stepcodeTextarea320").val())); // window.frames["iframe320"].document.close(); // $(window.frames["iframe320"]).load(function(){ // $("#iframe320").height($("#iframe320").contents().find("body").height()+showittryitheight); // }); // code320 = newCode; // } }); $(".tryButton320").click(function(){ $("#tryDiv320").show(); $("#stepcodeTextarea320").focus(); $("#stepcodeTextarea320").height(200); $("#iframe320").height(0); window.frames["iframe320"].document.write("
"); window.frames["iframe320"].document.write(decodeHtml($("textarea#stepcodeTextarea320").val())); window.frames["iframe320"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe320"]).load(function(){ $("#iframe320").height($("#iframe320").contents().find("body").height()+showittryitheight); }); $("#iframe320").height($("#iframe320").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor320.focus(); editor320.setSize(null, "250"); $("#rendering320").hide(); $("#rendered320").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 editor320 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea320"), { 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); } } }); editor320.on("change",function(doc){ if(!binded320){ $(window).bind('beforeunload',function(){ binded320 = true; return "xxxx"; }); } var newCode = doc.getValue(); code320 = newCode; $("textarea#stepcodeTextarea320").val(newCode); if(alreadyWriteCode320!=code320){ lastModifedTime320 = new Date().getTime(); $("#rendering320").show(); $("#rendered320").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor320 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor320.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv320").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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


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


关于 前端部分-HTML-元素 的提问

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

上传截图