how2j.cn


工具版本兼容问题
<div>

<span>

这两种标签都是布局用的

这种标签本身没有任何显示效果

通常是用来结合css进行页面布局

示例 1 : 看不出任何效果   
示例 2 : div布局   
示例 3 : div和span的区别   

示例 1 :

看不出任何效果

div 和 span 看不出任何效果
"); window.frames["iframe_show352"].document.write(decodeHtml($("textarea#stepcodeTextarea352").val())); window.frames["iframe_show352"].document.close(); $(window.frames["iframe_show352"]).load(function(){ $("#iframe_show352").height($("#iframe_show352").contents().find("body").height()+showittryitheight); }); $("#iframe_show352").height($("#iframe_show352").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
这没有标签 <div> 这是一个div </div> <span>这是一个span</span>
这没有标签
<div> 这是一个div </div>
<span>这是一个span</span>
"); window.frames["iframe352"].document.write(decodeHtml(code352)); window.frames["iframe352"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe352"]).load(function(){ $("#iframe352").height($("#iframe352").contents().find("body").height()+showittryitheight); }); $("#iframe352").height($("#iframe352").contents().find("body").height()+showittryitheight); alreadyWriteCode352 = code352; $("#rendering352").hide(); $("#rendered352").show(); } var tRereshRetry2DemoPanel352 = setInterval(rereshRetry2DemoPanel352,1000); var binded352 = false; $("textarea#stepcodeTextarea352").keyup(function(){ if(!binded352){ $(window).bind('beforeunload',function(){ binded352 = true; return "xxxx"; }); } var newCode = $(this).val() code352 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code352!=newCode){ // window.frames["iframe352"].document.write("
"); // window.frames["iframe352"].document.write(decodeHtml($("textarea#stepcodeTextarea352").val())); // window.frames["iframe352"].document.close(); // $(window.frames["iframe352"]).load(function(){ // $("#iframe352").height($("#iframe352").contents().find("body").height()+showittryitheight); // }); // code352 = newCode; // } }); $(".tryButton352").click(function(){ $("#tryDiv352").show(); $("#stepcodeTextarea352").focus(); $("#stepcodeTextarea352").height(200); $("#iframe352").height(0); window.frames["iframe352"].document.write("
"); window.frames["iframe352"].document.write(decodeHtml($("textarea#stepcodeTextarea352").val())); window.frames["iframe352"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe352"]).load(function(){ $("#iframe352").height($("#iframe352").contents().find("body").height()+showittryitheight); }); $("#iframe352").height($("#iframe352").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor352.focus(); editor352.setSize(null, "250"); $("#rendering352").hide(); $("#rendered352").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 editor352 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea352"), { 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); } } }); editor352.on("change",function(doc){ if(!binded352){ $(window).bind('beforeunload',function(){ binded352 = true; return "xxxx"; }); } var newCode = doc.getValue(); code352 = newCode; $("textarea#stepcodeTextarea352").val(newCode); if(alreadyWriteCode352!=code352){ lastModifedTime352 = new Date().getTime(); $("#rendering352").show(); $("#rendered352").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor352 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor352.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv352").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

div布局

一个简单的div布局的例子
注: 这里使用了style外边距样式,margin-left:50px 指的是左边距50个像素
需要对两个图片进行左边距控制
如果不使用div,则需要对每一个图像设置边距
使用了div后,先把两个图像都放在一个div里
只需要设置div的边距,就可以达到两个图片都移动的效果
"); window.frames["iframe_show354"].document.write(decodeHtml($("textarea#stepcodeTextarea354").val())); window.frames["iframe_show354"].document.close(); $(window.frames["iframe_show354"]).load(function(){ $("#iframe_show354").height($("#iframe_show354").contents().find("body").height()+showittryitheight); }); $("#iframe_show354").height($("#iframe_show354").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<img style="margin-left:50px" src="http://127.0.0.1/example.gif"/> <br/> <img style="margin-left:50px" src="http://127.0.0.1/example.gif"/> <div style="margin-left:100px" > <img src="http://127.0.0.1/example.gif"/> <br/> <img src="http://127.0.0.1/example.gif"/> </div>
 <img style="margin-left:50px" src="http://127.0.0.1/example.gif"/>
  <br/>
 <img style="margin-left:50px" src="http://127.0.0.1/example.gif"/>

<div style="margin-left:100px" >
 <img src="http://127.0.0.1/example.gif"/>
  <br/>
 <img src="http://127.0.0.1/example.gif"/>
</div>
"); window.frames["iframe354"].document.write(decodeHtml(code354)); window.frames["iframe354"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe354"]).load(function(){ $("#iframe354").height($("#iframe354").contents().find("body").height()+showittryitheight); }); $("#iframe354").height($("#iframe354").contents().find("body").height()+showittryitheight); alreadyWriteCode354 = code354; $("#rendering354").hide(); $("#rendered354").show(); } var tRereshRetry2DemoPanel354 = setInterval(rereshRetry2DemoPanel354,1000); var binded354 = false; $("textarea#stepcodeTextarea354").keyup(function(){ if(!binded354){ $(window).bind('beforeunload',function(){ binded354 = true; return "xxxx"; }); } var newCode = $(this).val() code354 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code354!=newCode){ // window.frames["iframe354"].document.write("
"); // window.frames["iframe354"].document.write(decodeHtml($("textarea#stepcodeTextarea354").val())); // window.frames["iframe354"].document.close(); // $(window.frames["iframe354"]).load(function(){ // $("#iframe354").height($("#iframe354").contents().find("body").height()+showittryitheight); // }); // code354 = newCode; // } }); $(".tryButton354").click(function(){ $("#tryDiv354").show(); $("#stepcodeTextarea354").focus(); $("#stepcodeTextarea354").height(200); $("#iframe354").height(0); window.frames["iframe354"].document.write("
"); window.frames["iframe354"].document.write(decodeHtml($("textarea#stepcodeTextarea354").val())); window.frames["iframe354"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe354"]).load(function(){ $("#iframe354").height($("#iframe354").contents().find("body").height()+showittryitheight); }); $("#iframe354").height($("#iframe354").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor354.focus(); editor354.setSize(null, "250"); $("#rendering354").hide(); $("#rendered354").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 editor354 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea354"), { 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); } } }); editor354.on("change",function(doc){ if(!binded354){ $(window).bind('beforeunload',function(){ binded354 = true; return "xxxx"; }); } var newCode = doc.getValue(); code354 = newCode; $("textarea#stepcodeTextarea354").val(newCode); if(alreadyWriteCode354!=code354){ lastModifedTime354 = new Date().getTime(); $("#rendering354").show(); $("#rendered354").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor354 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor354.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv354").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

div和span的区别

div是块元素,即自动换行
常见的块元素还有h1,table,p
span是内联元素,即不会换行
常见的内联元素还有img,a,b,strong
"); window.frames["iframe_show353"].document.write(decodeHtml($("textarea#stepcodeTextarea353").val())); window.frames["iframe_show353"].document.close(); $(window.frames["iframe_show353"]).load(function(){ $("#iframe_show353").height($("#iframe_show353").contents().find("body").height()+showittryitheight); }); $("#iframe_show353").height($("#iframe_show353").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<div> 第一个div </div> <div> 第二个div </div> <span> 第一个span </span> <span> 第二个span </span>
<div>
 第一个div
</div>

<div>
 第二个div
</div>

<span>
 第一个span
</span>

<span>
 第二个span
</span>
"); window.frames["iframe353"].document.write(decodeHtml(code353)); window.frames["iframe353"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe353"]).load(function(){ $("#iframe353").height($("#iframe353").contents().find("body").height()+showittryitheight); }); $("#iframe353").height($("#iframe353").contents().find("body").height()+showittryitheight); alreadyWriteCode353 = code353; $("#rendering353").hide(); $("#rendered353").show(); } var tRereshRetry2DemoPanel353 = setInterval(rereshRetry2DemoPanel353,1000); var binded353 = false; $("textarea#stepcodeTextarea353").keyup(function(){ if(!binded353){ $(window).bind('beforeunload',function(){ binded353 = true; return "xxxx"; }); } var newCode = $(this).val() code353 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code353!=newCode){ // window.frames["iframe353"].document.write("
"); // window.frames["iframe353"].document.write(decodeHtml($("textarea#stepcodeTextarea353").val())); // window.frames["iframe353"].document.close(); // $(window.frames["iframe353"]).load(function(){ // $("#iframe353").height($("#iframe353").contents().find("body").height()+showittryitheight); // }); // code353 = newCode; // } }); $(".tryButton353").click(function(){ $("#tryDiv353").show(); $("#stepcodeTextarea353").focus(); $("#stepcodeTextarea353").height(200); $("#iframe353").height(0); window.frames["iframe353"].document.write("
"); window.frames["iframe353"].document.write(decodeHtml($("textarea#stepcodeTextarea353").val())); window.frames["iframe353"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe353"]).load(function(){ $("#iframe353").height($("#iframe353").contents().find("body").height()+showittryitheight); }); $("#iframe353").height($("#iframe353").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor353.focus(); editor353.setSize(null, "250"); $("#rendering353").hide(); $("#rendered353").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 editor353 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea353"), { 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); } } }); editor353.on("change",function(doc){ if(!binded353){ $(window).bind('beforeunload',function(){ binded353 = true; return "xxxx"; }); } var newCode = doc.getValue(); code353 = newCode; $("textarea#stepcodeTextarea353").val(newCode); if(alreadyWriteCode353!=code353){ lastModifedTime353 = new Date().getTime(); $("#rendering353").show(); $("#rendered353").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor353 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor353.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv353").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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


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


关于 前端部分-HTML-块 div span 的提问

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

上传截图