how2j.cn

相关下载
文件名 文件大小
example.gif 94k

工具版本兼容问题
<img> 即图像标签
需要设置其属性 src指定图像的路径

示例 1 : 显示图像   
示例 2 : 同级目录图像   
示例 3 : 上级目录图像   
示例 4 : 其他目录图像   
示例 5 : 设置图像的大小   
示例 6 : 图像居中   
示例 7 : 替换图片上的文字   

示例 1 :

显示图像

"); window.frames["iframe_show330"].document.write(decodeHtml($("textarea#stepcodeTextarea330").val())); window.frames["iframe_show330"].document.close(); $(window.frames["iframe_show330"]).load(function(){ $("#iframe_show330").height($("#iframe_show330").contents().find("body").height()+showittryitheight); }); $("#iframe_show330").height($("#iframe_show330").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<img src="http://127.0.0.1/example.gif"/>
<img src="http://127.0.0.1/example.gif"/>
"); window.frames["iframe330"].document.write(decodeHtml(code330)); window.frames["iframe330"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe330"]).load(function(){ $("#iframe330").height($("#iframe330").contents().find("body").height()+showittryitheight); }); $("#iframe330").height($("#iframe330").contents().find("body").height()+showittryitheight); alreadyWriteCode330 = code330; $("#rendering330").hide(); $("#rendered330").show(); } var tRereshRetry2DemoPanel330 = setInterval(rereshRetry2DemoPanel330,1000); var binded330 = false; $("textarea#stepcodeTextarea330").keyup(function(){ if(!binded330){ $(window).bind('beforeunload',function(){ binded330 = true; return "xxxx"; }); } var newCode = $(this).val() code330 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code330!=newCode){ // window.frames["iframe330"].document.write("
"); // window.frames["iframe330"].document.write(decodeHtml($("textarea#stepcodeTextarea330").val())); // window.frames["iframe330"].document.close(); // $(window.frames["iframe330"]).load(function(){ // $("#iframe330").height($("#iframe330").contents().find("body").height()+showittryitheight); // }); // code330 = newCode; // } }); $(".tryButton330").click(function(){ $("#tryDiv330").show(); $("#stepcodeTextarea330").focus(); $("#stepcodeTextarea330").height(200); $("#iframe330").height(0); window.frames["iframe330"].document.write("
"); window.frames["iframe330"].document.write(decodeHtml($("textarea#stepcodeTextarea330").val())); window.frames["iframe330"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe330"]).load(function(){ $("#iframe330").height($("#iframe330").contents().find("body").height()+showittryitheight); }); $("#iframe330").height($("#iframe330").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor330.focus(); editor330.setSize(null, "250"); $("#rendering330").hide(); $("#rendered330").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 editor330 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea330"), { 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); } } }); editor330.on("change",function(doc){ if(!binded330){ $(window).bind('beforeunload',function(){ binded330 = true; return "xxxx"; }); } var newCode = doc.getValue(); code330 = newCode; $("textarea#stepcodeTextarea330").val(newCode); if(alreadyWriteCode330!=code330){ lastModifedTime330 = new Date().getTime(); $("#rendering330").show(); $("#rendered330").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor330 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor330.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv330").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

同级目录图像

如果是本地文件,只需把图片放在同一个目录下即可
src直接使用文件名,不需要/
同级目录图像
示例 3 :

上级目录图像

图片在上级目录,则在src上加上 ../,即可访问上级目录的图片
如果是在上级目录的上级目录,则使用 ../../
上级目录图像
示例 4 :

其他目录图像

src使用图片所在的绝对路径,并在前面加上file://
其他目录图像
示例 5 :

设置图像的大小

如果设置的大小比原图片大,则会产生失真效果
"); window.frames["iframe_show333"].document.write(decodeHtml($("textarea#stepcodeTextarea333").val())); window.frames["iframe_show333"].document.close(); $(window.frames["iframe_show333"]).load(function(){ $("#iframe_show333").height($("#iframe_show333").contents().find("body").height()+showittryitheight); }); $("#iframe_show333").height($("#iframe_show333").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<img width="200" height="200" src="http://127.0.0.1/example.gif"/>
<img width="200" height="200" src="http://127.0.0.1/example.gif"/>
"); window.frames["iframe333"].document.write(decodeHtml(code333)); window.frames["iframe333"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe333"]).load(function(){ $("#iframe333").height($("#iframe333").contents().find("body").height()+showittryitheight); }); $("#iframe333").height($("#iframe333").contents().find("body").height()+showittryitheight); alreadyWriteCode333 = code333; $("#rendering333").hide(); $("#rendered333").show(); } var tRereshRetry2DemoPanel333 = setInterval(rereshRetry2DemoPanel333,1000); var binded333 = false; $("textarea#stepcodeTextarea333").keyup(function(){ if(!binded333){ $(window).bind('beforeunload',function(){ binded333 = true; return "xxxx"; }); } var newCode = $(this).val() code333 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code333!=newCode){ // window.frames["iframe333"].document.write("
"); // window.frames["iframe333"].document.write(decodeHtml($("textarea#stepcodeTextarea333").val())); // window.frames["iframe333"].document.close(); // $(window.frames["iframe333"]).load(function(){ // $("#iframe333").height($("#iframe333").contents().find("body").height()+showittryitheight); // }); // code333 = newCode; // } }); $(".tryButton333").click(function(){ $("#tryDiv333").show(); $("#stepcodeTextarea333").focus(); $("#stepcodeTextarea333").height(200); $("#iframe333").height(0); window.frames["iframe333"].document.write("
"); window.frames["iframe333"].document.write(decodeHtml($("textarea#stepcodeTextarea333").val())); window.frames["iframe333"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe333"]).load(function(){ $("#iframe333").height($("#iframe333").contents().find("body").height()+showittryitheight); }); $("#iframe333").height($("#iframe333").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor333.focus(); editor333.setSize(null, "250"); $("#rendering333").hide(); $("#rendered333").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 editor333 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea333"), { 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); } } }); editor333.on("change",function(doc){ if(!binded333){ $(window).bind('beforeunload',function(){ binded333 = true; return "xxxx"; }); } var newCode = doc.getValue(); code333 = newCode; $("textarea#stepcodeTextarea333").val(newCode); if(alreadyWriteCode333!=code333){ lastModifedTime333 = new Date().getTime(); $("#rendering333").show(); $("#rendered333").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor333 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor333.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv333").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

图像居中

img不能够自己居中,需要放在其他能够居中的标签中实现这个效果,比如h1标签,p标签.
经常采用的手段是放在div中居中
关于div,请参考
"); window.frames["iframe_show335"].document.write(decodeHtml($("textarea#stepcodeTextarea335").val())); window.frames["iframe_show335"].document.close(); $(window.frames["iframe_show335"]).load(function(){ $("#iframe_show335").height($("#iframe_show335").contents().find("body").height()+showittryitheight); }); $("#iframe_show335").height($("#iframe_show335").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<div align="left"> <img src="http://127.0.0.1/example.gif"/> </div> <div align="center"> <img src="http://127.0.0.1/example.gif"/> </div> <div align="right"> <img src="http://127.0.0.1/example.gif"/> </div>
<div align="left">
  <img src="http://127.0.0.1/example.gif"/>
</div>

<div align="center">
  <img src="http://127.0.0.1/example.gif"/>
</div>

<div align="right">
  <img src="http://127.0.0.1/example.gif"/>
</div>
"); window.frames["iframe335"].document.write(decodeHtml(code335)); window.frames["iframe335"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe335"]).load(function(){ $("#iframe335").height($("#iframe335").contents().find("body").height()+showittryitheight); }); $("#iframe335").height($("#iframe335").contents().find("body").height()+showittryitheight); alreadyWriteCode335 = code335; $("#rendering335").hide(); $("#rendered335").show(); } var tRereshRetry2DemoPanel335 = setInterval(rereshRetry2DemoPanel335,1000); var binded335 = false; $("textarea#stepcodeTextarea335").keyup(function(){ if(!binded335){ $(window).bind('beforeunload',function(){ binded335 = true; return "xxxx"; }); } var newCode = $(this).val() code335 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code335!=newCode){ // window.frames["iframe335"].document.write("
"); // window.frames["iframe335"].document.write(decodeHtml($("textarea#stepcodeTextarea335").val())); // window.frames["iframe335"].document.close(); // $(window.frames["iframe335"]).load(function(){ // $("#iframe335").height($("#iframe335").contents().find("body").height()+showittryitheight); // }); // code335 = newCode; // } }); $(".tryButton335").click(function(){ $("#tryDiv335").show(); $("#stepcodeTextarea335").focus(); $("#stepcodeTextarea335").height(200); $("#iframe335").height(0); window.frames["iframe335"].document.write("
"); window.frames["iframe335"].document.write(decodeHtml($("textarea#stepcodeTextarea335").val())); window.frames["iframe335"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe335"]).load(function(){ $("#iframe335").height($("#iframe335").contents().find("body").height()+showittryitheight); }); $("#iframe335").height($("#iframe335").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor335.focus(); editor335.setSize(null, "250"); $("#rendering335").hide(); $("#rendered335").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 editor335 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea335"), { 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); } } }); editor335.on("change",function(doc){ if(!binded335){ $(window).bind('beforeunload',function(){ binded335 = true; return "xxxx"; }); } var newCode = doc.getValue(); code335 = newCode; $("textarea#stepcodeTextarea335").val(newCode); if(alreadyWriteCode335!=code335){ lastModifedTime335 = new Date().getTime(); $("#rendering335").show(); $("#rendered335").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor335 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor335.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv335").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

替换图片上的文字

如果图片不存在,默认会显示一个缺失图片,这是不友好的
所以可以加上alt属性。
当图片存在的时候,alt是不会显示的
当图片不存在的时候,alt就会出现
"); window.frames["iframe_show338"].document.write(decodeHtml($("textarea#stepcodeTextarea338").val())); window.frames["iframe_show338"].document.close(); $(window.frames["iframe_show338"]).load(function(){ $("#iframe_show338").height($("#iframe_show338").contents().find("body").height()+showittryitheight); }); $("#iframe_show338").height($("#iframe_show338").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<img src="http://127.0.0.1/example_not_exist.gif" /> <br/> <img src="http://127.0.0.1/example.gif" alt="这个是一个图片" /> <br/> <img src="http://127.0.0.1/example_not_exist.gif" alt="这个是一个图片" />
  <img src="http://127.0.0.1/example_not_exist.gif" />

  <br/>

  <img src="http://127.0.0.1/example.gif" alt="这个是一个图片" />

  <br/>
  <img src="http://127.0.0.1/example_not_exist.gif" alt="这个是一个图片" />
"); window.frames["iframe338"].document.write(decodeHtml(code338)); window.frames["iframe338"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe338"]).load(function(){ $("#iframe338").height($("#iframe338").contents().find("body").height()+showittryitheight); }); $("#iframe338").height($("#iframe338").contents().find("body").height()+showittryitheight); alreadyWriteCode338 = code338; $("#rendering338").hide(); $("#rendered338").show(); } var tRereshRetry2DemoPanel338 = setInterval(rereshRetry2DemoPanel338,1000); var binded338 = false; $("textarea#stepcodeTextarea338").keyup(function(){ if(!binded338){ $(window).bind('beforeunload',function(){ binded338 = true; return "xxxx"; }); } var newCode = $(this).val() code338 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code338!=newCode){ // window.frames["iframe338"].document.write("
"); // window.frames["iframe338"].document.write(decodeHtml($("textarea#stepcodeTextarea338").val())); // window.frames["iframe338"].document.close(); // $(window.frames["iframe338"]).load(function(){ // $("#iframe338").height($("#iframe338").contents().find("body").height()+showittryitheight); // }); // code338 = newCode; // } }); $(".tryButton338").click(function(){ $("#tryDiv338").show(); $("#stepcodeTextarea338").focus(); $("#stepcodeTextarea338").height(200); $("#iframe338").height(0); window.frames["iframe338"].document.write("
"); window.frames["iframe338"].document.write(decodeHtml($("textarea#stepcodeTextarea338").val())); window.frames["iframe338"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe338"]).load(function(){ $("#iframe338").height($("#iframe338").contents().find("body").height()+showittryitheight); }); $("#iframe338").height($("#iframe338").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor338.focus(); editor338.setSize(null, "250"); $("#rendering338").hide(); $("#rendered338").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 editor338 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea338"), { 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); } } }); editor338.on("change",function(doc){ if(!binded338){ $(window).bind('beforeunload',function(){ binded338 = true; return "xxxx"; }); } var newCode = doc.getValue(); code338 = newCode; $("textarea#stepcodeTextarea338").val(newCode); if(alreadyWriteCode338!=code338){ lastModifedTime338 = new Date().getTime(); $("#rendering338").show(); $("#rendered338").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor338 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor338.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv338").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

上传截图