|
工具版本兼容问题
学习到这里已经使用了大量的JQuery对象的方法,比如 show() 、toggle()等等,这些方法都是属于JQuery对象的,而原生的DOM对象不具备这些方法。
同样的 DOM对象也具备JQuery对象所没有的属性和方法,比如checked属性。
在某些场景下,需要对JQuery对象和DOM节点对象进行互相转换
关键字 |
简介 |
示例代码 |
get(0) [0]
|
JQuery转DOM
|
示例代码
|
$()
|
DOM转JQuery
|
示例代码
|
示例
1
:
JQuery转DOM
示例
2
:
DOM转JQuery
通过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>
<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 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
");
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>
<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 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
代码高亮插件双击即可选中,不过部分同学反应,通过代码高亮插件复制的代码无法在IDEA里正常显示,这里提供TEXTAREA的方式,方便复制,谢谢
1. 自行完成练习
根据练习目标尽量自己实现代码效果,期间会碰到疑问,难题,和自己不懂的地方,这些都是必要的过程
2. 带着疑问查看答案
完成过程中,碰到无法解决的问题,带着疑问,查看答案,分析答案的解决思路
3. 查看答案讲解视频
依然有不明白的地方,点开视频讲解,带着疑问,听视频讲解有问题的部分
4. 再做一遍
理解后,再从头做一遍,把有疑问的地方都捋清楚
5. 总结
最后再总结一遍,总结思路,总结解决办法,以后遇到类似的问题,怎么处理
HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。
提问之前请登陆
|