how2j.cn

关键字 简介 示例代码
val
取值
示例代码
html
获取元素内容,如果有子元素,保留标签
示例代码
text
获取元素内容,如果有子元素,不包含子元素标签
示例代码
示例 1 : 取值   
示例 2 : 获取元素内容,如果有子元素,保留标签   
示例 3 : 获取元素内容,如果有子元素,不包含子元素标签   

示例 1 :

取值

通过JQuery对象的val()方法获取值
相当于 document.getElementById("input1").value;
"); window.frames["iframe_show1220"].document.write(decodeHtml($("textarea#stepcodeTextarea1220").val())); window.frames["iframe_show1220"].document.close(); $(window.frames["iframe_show1220"]).load(function(){ $("#iframe_show1220").height($("#iframe_show1220").contents().find("body").height()+showittryitheight); }); $("#iframe_show1220").height($("#iframe_show1220").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/study/jquery.min.js"></script> <script> $(function(){ $("#b1").click(function(){ alert($("#input1").val()); }); }); </script> <button id="b1">取值</button> <br> <br> <input type="text" id="input1" value="默认值">
<script src="http://127.0.0.1/study/jquery.min.js"></script>
 
<script>
$(function(){
   $("#b1").click(function(){
      alert($("#input1").val());
   });
});
  
</script>
  
<button id="b1">取值</button>
   
<br>
<br>
  
<input type="text" id="input1" value="默认值">
"); window.frames["iframe1220"].document.write(decodeHtml(code1220)); window.frames["iframe1220"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1220"]).load(function(){ $("#iframe1220").height($("#iframe1220").contents().find("body").height()+showittryitheight); }); $("#iframe1220").height($("#iframe1220").contents().find("body").height()+showittryitheight); alreadyWriteCode1220 = code1220; $("#rendering1220").hide(); $("#rendered1220").show(); } var tRereshRetry2DemoPanel1220 = setInterval(rereshRetry2DemoPanel1220,1000); var binded1220 = false; $("textarea#stepcodeTextarea1220").keyup(function(){ if(!binded1220){ $(window).bind('beforeunload',function(){ binded1220 = true; return "xxxx"; }); } var newCode = $(this).val() code1220 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1220!=newCode){ // window.frames["iframe1220"].document.write("
"); // window.frames["iframe1220"].document.write(decodeHtml($("textarea#stepcodeTextarea1220").val())); // window.frames["iframe1220"].document.close(); // $(window.frames["iframe1220"]).load(function(){ // $("#iframe1220").height($("#iframe1220").contents().find("body").height()+showittryitheight); // }); // code1220 = newCode; // } }); $(".tryButton1220").click(function(){ $("#tryDiv1220").show(); $("#stepcodeTextarea1220").focus(); $("#stepcodeTextarea1220").height(200); $("#iframe1220").height(0); window.frames["iframe1220"].document.write("
"); window.frames["iframe1220"].document.write(decodeHtml($("textarea#stepcodeTextarea1220").val())); window.frames["iframe1220"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1220"]).load(function(){ $("#iframe1220").height($("#iframe1220").contents().find("body").height()+showittryitheight); }); $("#iframe1220").height($("#iframe1220").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1220.focus(); editor1220.setSize(null, "250"); $("#rendering1220").hide(); $("#rendered1220").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 editor1220 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1220"), { 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); } } }); editor1220.on("change",function(doc){ if(!binded1220){ $(window).bind('beforeunload',function(){ binded1220 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1220 = newCode; $("textarea#stepcodeTextarea1220").val(newCode); if(alreadyWriteCode1220!=code1220){ lastModifedTime1220 = new Date().getTime(); $("#rendering1220").show(); $("#rendered1220").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1220 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1220.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1220").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

获取元素内容,如果有子元素,保留标签

通过html() 获取元素内容,如果有子元素,保留标签
"); window.frames["iframe_show1221"].document.write(decodeHtml($("textarea#stepcodeTextarea1221").val())); window.frames["iframe_show1221"].document.close(); $(window.frames["iframe_show1221"]).load(function(){ $("#iframe_show1221").height($("#iframe_show1221").contents().find("body").height()+showittryitheight); }); $("#iframe_show1221").height($("#iframe_show1221").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/study/jquery.min.js"></script> <script> $(function(){ $("#b1").click(function(){ alert($("#d1").html()); }); }); </script> <button id="b1">获取文本内容</button> <br> <br> <div id="d1"> 这是div的内容 <span>这是div里的span </span> </div>
"); window.frames["iframe1221"].document.write(decodeHtml(code1221)); window.frames["iframe1221"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1221"]).load(function(){ $("#iframe1221").height($("#iframe1221").contents().find("body").height()+showittryitheight); }); $("#iframe1221").height($("#iframe1221").contents().find("body").height()+showittryitheight); alreadyWriteCode1221 = code1221; $("#rendering1221").hide(); $("#rendered1221").show(); } var tRereshRetry2DemoPanel1221 = setInterval(rereshRetry2DemoPanel1221,1000); var binded1221 = false; $("textarea#stepcodeTextarea1221").keyup(function(){ if(!binded1221){ $(window).bind('beforeunload',function(){ binded1221 = true; return "xxxx"; }); } var newCode = $(this).val() code1221 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1221!=newCode){ // window.frames["iframe1221"].document.write("
"); // window.frames["iframe1221"].document.write(decodeHtml($("textarea#stepcodeTextarea1221").val())); // window.frames["iframe1221"].document.close(); // $(window.frames["iframe1221"]).load(function(){ // $("#iframe1221").height($("#iframe1221").contents().find("body").height()+showittryitheight); // }); // code1221 = newCode; // } }); $(".tryButton1221").click(function(){ $("#tryDiv1221").show(); $("#stepcodeTextarea1221").focus(); $("#stepcodeTextarea1221").height(200); $("#iframe1221").height(0); window.frames["iframe1221"].document.write("
"); window.frames["iframe1221"].document.write(decodeHtml($("textarea#stepcodeTextarea1221").val())); window.frames["iframe1221"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1221"]).load(function(){ $("#iframe1221").height($("#iframe1221").contents().find("body").height()+showittryitheight); }); $("#iframe1221").height($("#iframe1221").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1221.focus(); editor1221.setSize(null, "250"); $("#rendering1221").hide(); $("#rendered1221").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 editor1221 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1221"), { 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); } } }); editor1221.on("change",function(doc){ if(!binded1221){ $(window).bind('beforeunload',function(){ binded1221 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1221 = newCode; $("textarea#stepcodeTextarea1221").val(newCode); if(alreadyWriteCode1221!=code1221){ lastModifedTime1221 = new Date().getTime(); $("#rendering1221").show(); $("#rendered1221").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1221 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1221.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1221").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

获取元素内容,如果有子元素,不包含子元素标签

通过text() 获取元素内容,如果有子元素,不包含标签
"); window.frames["iframe_show1222"].document.write(decodeHtml($("textarea#stepcodeTextarea1222").val())); window.frames["iframe_show1222"].document.close(); $(window.frames["iframe_show1222"]).load(function(){ $("#iframe_show1222").height($("#iframe_show1222").contents().find("body").height()+showittryitheight); }); $("#iframe_show1222").height($("#iframe_show1222").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/study/jquery.min.js"></script> <script> $(function(){ $("#b1").click(function(){ alert($("#d1").text()); }); }); </script> <button id="b1">获取文本内容</button> <br> <br> <div id="d1"> 这是div的内容 <span>这是div里的span </span> </div>
"); window.frames["iframe1222"].document.write(decodeHtml(code1222)); window.frames["iframe1222"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1222"]).load(function(){ $("#iframe1222").height($("#iframe1222").contents().find("body").height()+showittryitheight); }); $("#iframe1222").height($("#iframe1222").contents().find("body").height()+showittryitheight); alreadyWriteCode1222 = code1222; $("#rendering1222").hide(); $("#rendered1222").show(); } var tRereshRetry2DemoPanel1222 = setInterval(rereshRetry2DemoPanel1222,1000); var binded1222 = false; $("textarea#stepcodeTextarea1222").keyup(function(){ if(!binded1222){ $(window).bind('beforeunload',function(){ binded1222 = true; return "xxxx"; }); } var newCode = $(this).val() code1222 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1222!=newCode){ // window.frames["iframe1222"].document.write("
"); // window.frames["iframe1222"].document.write(decodeHtml($("textarea#stepcodeTextarea1222").val())); // window.frames["iframe1222"].document.close(); // $(window.frames["iframe1222"]).load(function(){ // $("#iframe1222").height($("#iframe1222").contents().find("body").height()+showittryitheight); // }); // code1222 = newCode; // } }); $(".tryButton1222").click(function(){ $("#tryDiv1222").show(); $("#stepcodeTextarea1222").focus(); $("#stepcodeTextarea1222").height(200); $("#iframe1222").height(0); window.frames["iframe1222"].document.write("
"); window.frames["iframe1222"].document.write(decodeHtml($("textarea#stepcodeTextarea1222").val())); window.frames["iframe1222"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1222"]).load(function(){ $("#iframe1222").height($("#iframe1222").contents().find("body").height()+showittryitheight); }); $("#iframe1222").height($("#iframe1222").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1222.focus(); editor1222.setSize(null, "250"); $("#rendering1222").hide(); $("#rendered1222").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 editor1222 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1222"), { 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); } } }); editor1222.on("change",function(doc){ if(!binded1222){ $(window).bind('beforeunload',function(){ binded1222 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1222 = newCode; $("textarea#stepcodeTextarea1222").val(newCode); if(alreadyWriteCode1222!=code1222){ lastModifedTime1222 = new Date().getTime(); $("#rendering1222").show(); $("#rendered1222").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1222 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1222.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1222").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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


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


关于 前端部分-JQuery-常见方法 的提问

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

上传截图