how2j.cn


工具版本兼容问题
学习到这里已经使用了大量的JQuery对象的方法,比如show() 、toggle()等等,这些方法都是属于JQuery对象的,而原生的DOM对象不具备这些方法。

同样的DOM对象也具备JQuery对象所没有的属性和方法,比如checked属性。

在某些场景下,需要对JQuery对象和DOM节点对象进行互相转换

关键字 简介 示例代码
get(0)
[0]
JQuery转DOM
示例代码
$()
DOM转JQuery
示例代码
示例 1 : JQuery转DOM   
示例 2 : DOM转JQuery   

示例 1 :

JQuery转DOM

通过get(0)或者[0] 把JQuery对象转为DOM对象
"); window.frames["iframe_show1226"].document.write(decodeHtml($("textarea#stepcodeTextarea1226").val())); window.frames["iframe_show1226"].document.close(); $(window.frames["iframe_show1226"]).load(function(){ $("#iframe_show1226").height($("#iframe_show1226").contents().find("body").height()+showittryitheight); }); $("#iframe_show1226").height($("#iframe_show1226").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(){ var div= $("#d"); var d = div[0]; var s ="JQuery对象是 " + div; s +="\n对应的DOM对象是 " + d alert(s); }); }); </script> <button id="b1">JQuery对象div转为DOM对象d</button> <br> <br> <style> .pink{ background-color:pink; } </style> <div id="d"> Hello JQuery </div>
"); window.frames["iframe1226"].document.write(decodeHtml(code1226)); window.frames["iframe1226"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1226"]).load(function(){ $("#iframe1226").height($("#iframe1226").contents().find("body").height()+showittryitheight); }); $("#iframe1226").height($("#iframe1226").contents().find("body").height()+showittryitheight); alreadyWriteCode1226 = code1226; $("#rendering1226").hide(); $("#rendered1226").show(); } var tRereshRetry2DemoPanel1226 = setInterval(rereshRetry2DemoPanel1226,1000); var binded1226 = false; $("textarea#stepcodeTextarea1226").keyup(function(){ if(!binded1226){ $(window).bind('beforeunload',function(){ binded1226 = true; return "xxxx"; }); } var newCode = $(this).val() code1226 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1226!=newCode){ // window.frames["iframe1226"].document.write("
"); // window.frames["iframe1226"].document.write(decodeHtml($("textarea#stepcodeTextarea1226").val())); // window.frames["iframe1226"].document.close(); // $(window.frames["iframe1226"]).load(function(){ // $("#iframe1226").height($("#iframe1226").contents().find("body").height()+showittryitheight); // }); // code1226 = newCode; // } }); $(".tryButton1226").click(function(){ $("#tryDiv1226").show(); $("#stepcodeTextarea1226").focus(); $("#stepcodeTextarea1226").height(200); $("#iframe1226").height(0); window.frames["iframe1226"].document.write("
"); window.frames["iframe1226"].document.write(decodeHtml($("textarea#stepcodeTextarea1226").val())); window.frames["iframe1226"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1226"]).load(function(){ $("#iframe1226").height($("#iframe1226").contents().find("body").height()+showittryitheight); }); $("#iframe1226").height($("#iframe1226").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1226.focus(); editor1226.setSize(null, "250"); $("#rendering1226").hide(); $("#rendered1226").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 editor1226 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1226"), { 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); } } }); editor1226.on("change",function(doc){ if(!binded1226){ $(window).bind('beforeunload',function(){ binded1226 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1226 = newCode; $("textarea#stepcodeTextarea1226").val(newCode); if(alreadyWriteCode1226!=code1226){ lastModifedTime1226 = new Date().getTime(); $("#rendering1226").show(); $("#rendered1226").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1226 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1226.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1226").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

DOM转JQuery

通过$() 把DOM对象转为JQuery对象
"); window.frames["iframe_show1227"].document.write(decodeHtml($("textarea#stepcodeTextarea1227").val())); window.frames["iframe_show1227"].document.close(); $(window.frames["iframe_show1227"]).load(function(){ $("#iframe_show1227").height($("#iframe_show1227").contents().find("body").height()+showittryitheight); }); $("#iframe_show1227").height($("#iframe_show1227").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(){ var div= document.getElementById("d"); var d = $(div); var s ="DOM对象是 " + div; s +="\n对应的JQuery对象是 " + d alert(s); }); }); </script> <button id="b1">DOM对象div转为JQuery对象d</button> <br> <br> <style> .pink{ background-color:pink; } </style> <div id="d"> Hello JQuery </div>
"); window.frames["iframe1227"].document.write(decodeHtml(code1227)); window.frames["iframe1227"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1227"]).load(function(){ $("#iframe1227").height($("#iframe1227").contents().find("body").height()+showittryitheight); }); $("#iframe1227").height($("#iframe1227").contents().find("body").height()+showittryitheight); alreadyWriteCode1227 = code1227; $("#rendering1227").hide(); $("#rendered1227").show(); } var tRereshRetry2DemoPanel1227 = setInterval(rereshRetry2DemoPanel1227,1000); var binded1227 = false; $("textarea#stepcodeTextarea1227").keyup(function(){ if(!binded1227){ $(window).bind('beforeunload',function(){ binded1227 = true; return "xxxx"; }); } var newCode = $(this).val() code1227 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1227!=newCode){ // window.frames["iframe1227"].document.write("
"); // window.frames["iframe1227"].document.write(decodeHtml($("textarea#stepcodeTextarea1227").val())); // window.frames["iframe1227"].document.close(); // $(window.frames["iframe1227"]).load(function(){ // $("#iframe1227").height($("#iframe1227").contents().find("body").height()+showittryitheight); // }); // code1227 = newCode; // } }); $(".tryButton1227").click(function(){ $("#tryDiv1227").show(); $("#stepcodeTextarea1227").focus(); $("#stepcodeTextarea1227").height(200); $("#iframe1227").height(0); window.frames["iframe1227"].document.write("
"); window.frames["iframe1227"].document.write(decodeHtml($("textarea#stepcodeTextarea1227").val())); window.frames["iframe1227"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1227"]).load(function(){ $("#iframe1227").height($("#iframe1227").contents().find("body").height()+showittryitheight); }); $("#iframe1227").height($("#iframe1227").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1227.focus(); editor1227.setSize(null, "250"); $("#rendering1227").hide(); $("#rendered1227").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 editor1227 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1227"), { 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); } } }); editor1227.on("change",function(doc){ if(!binded1227){ $(window).bind('beforeunload',function(){ binded1227 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1227 = newCode; $("textarea#stepcodeTextarea1227").val(newCode); if(alreadyWriteCode1227!=code1227){ lastModifedTime1227 = new Date().getTime(); $("#rendering1227").show(); $("#rendered1227").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1227 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1227.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1227").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

上传截图