how2j.cn


工具版本兼容问题
<input type="text"> 即表示文本框

并且只能够输入一行

如果要输入多行

使用文本域<textarea>

注: <input> 标签很特别,一般是不需要写成<input />或者<input></input> 这样的。
并且<input> 这样的写法也是满足标准的

示例 1 : 文本框   
示例 2 : 设置文本框的长度   
示例 3 : 初始值   
示例 4 : 有背景文字的文本框   

示例 1 :

文本框

"); window.frames["iframe_show355"].document.write(decodeHtml($("textarea#stepcodeTextarea355").val())); window.frames["iframe_show355"].document.close(); $(window.frames["iframe_show355"]).load(function(){ $("#iframe_show355").height($("#iframe_show355").contents().find("body").height()+showittryitheight); }); $("#iframe_show355").height($("#iframe_show355").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<input type="text">
<input type="text">
"); window.frames["iframe355"].document.write(decodeHtml(code355)); window.frames["iframe355"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe355"]).load(function(){ $("#iframe355").height($("#iframe355").contents().find("body").height()+showittryitheight); }); $("#iframe355").height($("#iframe355").contents().find("body").height()+showittryitheight); alreadyWriteCode355 = code355; $("#rendering355").hide(); $("#rendered355").show(); } var tRereshRetry2DemoPanel355 = setInterval(rereshRetry2DemoPanel355,1000); var binded355 = false; $("textarea#stepcodeTextarea355").keyup(function(){ if(!binded355){ $(window).bind('beforeunload',function(){ binded355 = true; return "xxxx"; }); } var newCode = $(this).val() code355 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code355!=newCode){ // window.frames["iframe355"].document.write("
"); // window.frames["iframe355"].document.write(decodeHtml($("textarea#stepcodeTextarea355").val())); // window.frames["iframe355"].document.close(); // $(window.frames["iframe355"]).load(function(){ // $("#iframe355").height($("#iframe355").contents().find("body").height()+showittryitheight); // }); // code355 = newCode; // } }); $(".tryButton355").click(function(){ $("#tryDiv355").show(); $("#stepcodeTextarea355").focus(); $("#stepcodeTextarea355").height(200); $("#iframe355").height(0); window.frames["iframe355"].document.write("
"); window.frames["iframe355"].document.write(decodeHtml($("textarea#stepcodeTextarea355").val())); window.frames["iframe355"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe355"]).load(function(){ $("#iframe355").height($("#iframe355").contents().find("body").height()+showittryitheight); }); $("#iframe355").height($("#iframe355").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor355.focus(); editor355.setSize(null, "250"); $("#rendering355").hide(); $("#rendered355").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 editor355 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea355"), { 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); } } }); editor355.on("change",function(doc){ if(!binded355){ $(window).bind('beforeunload',function(){ binded355 = true; return "xxxx"; }); } var newCode = doc.getValue(); code355 = newCode; $("textarea#stepcodeTextarea355").val(newCode); if(alreadyWriteCode355!=code355){ lastModifedTime355 = new Date().getTime(); $("#rendering355").show(); $("#rendered355").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor355 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor355.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv355").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

设置文本框的长度

使用属性size
"); window.frames["iframe_show356"].document.write(decodeHtml($("textarea#stepcodeTextarea356").val())); window.frames["iframe_show356"].document.close(); $(window.frames["iframe_show356"]).load(function(){ $("#iframe_show356").height($("#iframe_show356").contents().find("body").height()+showittryitheight); }); $("#iframe_show356").height($("#iframe_show356").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<input type="text" size="10">
<input type="text" size="10">
"); window.frames["iframe356"].document.write(decodeHtml(code356)); window.frames["iframe356"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe356"]).load(function(){ $("#iframe356").height($("#iframe356").contents().find("body").height()+showittryitheight); }); $("#iframe356").height($("#iframe356").contents().find("body").height()+showittryitheight); alreadyWriteCode356 = code356; $("#rendering356").hide(); $("#rendered356").show(); } var tRereshRetry2DemoPanel356 = setInterval(rereshRetry2DemoPanel356,1000); var binded356 = false; $("textarea#stepcodeTextarea356").keyup(function(){ if(!binded356){ $(window).bind('beforeunload',function(){ binded356 = true; return "xxxx"; }); } var newCode = $(this).val() code356 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code356!=newCode){ // window.frames["iframe356"].document.write("
"); // window.frames["iframe356"].document.write(decodeHtml($("textarea#stepcodeTextarea356").val())); // window.frames["iframe356"].document.close(); // $(window.frames["iframe356"]).load(function(){ // $("#iframe356").height($("#iframe356").contents().find("body").height()+showittryitheight); // }); // code356 = newCode; // } }); $(".tryButton356").click(function(){ $("#tryDiv356").show(); $("#stepcodeTextarea356").focus(); $("#stepcodeTextarea356").height(200); $("#iframe356").height(0); window.frames["iframe356"].document.write("
"); window.frames["iframe356"].document.write(decodeHtml($("textarea#stepcodeTextarea356").val())); window.frames["iframe356"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe356"]).load(function(){ $("#iframe356").height($("#iframe356").contents().find("body").height()+showittryitheight); }); $("#iframe356").height($("#iframe356").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor356.focus(); editor356.setSize(null, "250"); $("#rendering356").hide(); $("#rendered356").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 editor356 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea356"), { 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); } } }); editor356.on("change",function(doc){ if(!binded356){ $(window).bind('beforeunload',function(){ binded356 = true; return "xxxx"; }); } var newCode = doc.getValue(); code356 = newCode; $("textarea#stepcodeTextarea356").val(newCode); if(alreadyWriteCode356!=code356){ lastModifedTime356 = new Date().getTime(); $("#rendering356").show(); $("#rendered356").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor356 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor356.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv356").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

初始值

使用属性value
"); window.frames["iframe_show358"].document.write(decodeHtml($("textarea#stepcodeTextarea358").val())); window.frames["iframe_show358"].document.close(); $(window.frames["iframe_show358"]).load(function(){ $("#iframe_show358").height($("#iframe_show358").contents().find("body").height()+showittryitheight); }); $("#iframe_show358").height($("#iframe_show358").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<input type="text" value="有初始值的文本框">
<input type="text" value="有初始值的文本框">
"); window.frames["iframe358"].document.write(decodeHtml(code358)); window.frames["iframe358"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe358"]).load(function(){ $("#iframe358").height($("#iframe358").contents().find("body").height()+showittryitheight); }); $("#iframe358").height($("#iframe358").contents().find("body").height()+showittryitheight); alreadyWriteCode358 = code358; $("#rendering358").hide(); $("#rendered358").show(); } var tRereshRetry2DemoPanel358 = setInterval(rereshRetry2DemoPanel358,1000); var binded358 = false; $("textarea#stepcodeTextarea358").keyup(function(){ if(!binded358){ $(window).bind('beforeunload',function(){ binded358 = true; return "xxxx"; }); } var newCode = $(this).val() code358 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code358!=newCode){ // window.frames["iframe358"].document.write("
"); // window.frames["iframe358"].document.write(decodeHtml($("textarea#stepcodeTextarea358").val())); // window.frames["iframe358"].document.close(); // $(window.frames["iframe358"]).load(function(){ // $("#iframe358").height($("#iframe358").contents().find("body").height()+showittryitheight); // }); // code358 = newCode; // } }); $(".tryButton358").click(function(){ $("#tryDiv358").show(); $("#stepcodeTextarea358").focus(); $("#stepcodeTextarea358").height(200); $("#iframe358").height(0); window.frames["iframe358"].document.write("
"); window.frames["iframe358"].document.write(decodeHtml($("textarea#stepcodeTextarea358").val())); window.frames["iframe358"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe358"]).load(function(){ $("#iframe358").height($("#iframe358").contents().find("body").height()+showittryitheight); }); $("#iframe358").height($("#iframe358").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor358.focus(); editor358.setSize(null, "250"); $("#rendering358").hide(); $("#rendered358").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 editor358 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea358"), { 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); } } }); editor358.on("change",function(doc){ if(!binded358){ $(window).bind('beforeunload',function(){ binded358 = true; return "xxxx"; }); } var newCode = doc.getValue(); code358 = newCode; $("textarea#stepcodeTextarea358").val(newCode); if(alreadyWriteCode358!=code358){ lastModifedTime358 = new Date().getTime(); $("#rendering358").show(); $("#rendered358").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor358 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor358.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv358").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

有背景文字的文本框

使用属性placeholder
placeholder是一个html5的属性,对于大多数的已经支持html5的浏览器来说,是可以看到效果的,但是对于老的不支持html5的浏览器,比如ie8,就看不到效果
"); window.frames["iframe_show359"].document.write(decodeHtml($("textarea#stepcodeTextarea359").val())); window.frames["iframe_show359"].document.close(); $(window.frames["iframe_show359"]).load(function(){ $("#iframe_show359").height($("#iframe_show359").contents().find("body").height()+showittryitheight); }); $("#iframe_show359").height($("#iframe_show359").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<input type="text" placeholder="请输入账号">
<input type="text" placeholder="请输入账号">
"); window.frames["iframe359"].document.write(decodeHtml(code359)); window.frames["iframe359"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe359"]).load(function(){ $("#iframe359").height($("#iframe359").contents().find("body").height()+showittryitheight); }); $("#iframe359").height($("#iframe359").contents().find("body").height()+showittryitheight); alreadyWriteCode359 = code359; $("#rendering359").hide(); $("#rendered359").show(); } var tRereshRetry2DemoPanel359 = setInterval(rereshRetry2DemoPanel359,1000); var binded359 = false; $("textarea#stepcodeTextarea359").keyup(function(){ if(!binded359){ $(window).bind('beforeunload',function(){ binded359 = true; return "xxxx"; }); } var newCode = $(this).val() code359 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code359!=newCode){ // window.frames["iframe359"].document.write("
"); // window.frames["iframe359"].document.write(decodeHtml($("textarea#stepcodeTextarea359").val())); // window.frames["iframe359"].document.close(); // $(window.frames["iframe359"]).load(function(){ // $("#iframe359").height($("#iframe359").contents().find("body").height()+showittryitheight); // }); // code359 = newCode; // } }); $(".tryButton359").click(function(){ $("#tryDiv359").show(); $("#stepcodeTextarea359").focus(); $("#stepcodeTextarea359").height(200); $("#iframe359").height(0); window.frames["iframe359"].document.write("
"); window.frames["iframe359"].document.write(decodeHtml($("textarea#stepcodeTextarea359").val())); window.frames["iframe359"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe359"]).load(function(){ $("#iframe359").height($("#iframe359").contents().find("body").height()+showittryitheight); }); $("#iframe359").height($("#iframe359").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor359.focus(); editor359.setSize(null, "250"); $("#rendering359").hide(); $("#rendered359").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 editor359 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea359"), { 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); } } }); editor359.on("change",function(doc){ if(!binded359){ $(window).bind('beforeunload',function(){ binded359 = true; return "xxxx"; }); } var newCode = doc.getValue(); code359 = newCode; $("textarea#stepcodeTextarea359").val(newCode); if(alreadyWriteCode359!=code359){ lastModifedTime359 = new Date().getTime(); $("#rendering359").show(); $("#rendered359").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor359 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor359.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv359").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

上传截图