|
工具版本兼容问题
在JavaScript中可以自定义对象,添加新的属性,添加新的方法
示例
1
:
通过new Object创建对象
示例
2
:
通过function设计一个对象
示例
3
:
为已经存在的对象,增加新的方法
示例
1
:
通过new Object创建对象
顶
折
");
window.frames["iframe_show1117"].document.write(decodeHtml($("textarea#stepcodeTextarea1117").val()));
window.frames["iframe_show1117"].document.close();
$(window.frames["iframe_show1117"]).load(function(){
$("#iframe_show1117").height($("#iframe_show1117").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1117").height($("#iframe_show1117").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
<script>
var hero = new Object();
hero.name = "盖伦"; //定义一个属性name,并且赋值
hero.kill = function(){
document.write(hero.name + " 正在杀敌" ); //定义一个函数kill
}
hero.kill(); //调用函数kill
</script>
<script>
var hero = new Object();
hero.name = "盖伦"; //定义一个属性name,并且赋值
hero.kill = function(){
document.write(hero.name + " 正在杀敌" ); //定义一个函数kill
}
hero.kill(); //调用函数kill
</script>
");
window.frames["iframe1117"].document.write(decodeHtml(code1117));
window.frames["iframe1117"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1117"]).load(function(){
$("#iframe1117").height($("#iframe1117").contents().find("body").height()+showittryitheight);
});
$("#iframe1117").height($("#iframe1117").contents().find("body").height()+showittryitheight);
alreadyWriteCode1117 = code1117;
$("#rendering1117").hide();
$("#rendered1117").show();
}
var tRereshRetry2DemoPanel1117 = setInterval(rereshRetry2DemoPanel1117,1000);
var binded1117 = false;
$("textarea#stepcodeTextarea1117").keyup(function(){
if(!binded1117){
$(window).bind('beforeunload',function(){
binded1117 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1117 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1117!=newCode){
// window.frames["iframe1117"].document.write("
");
// window.frames["iframe1117"].document.write(decodeHtml($("textarea#stepcodeTextarea1117").val()));
// window.frames["iframe1117"].document.close();
// $(window.frames["iframe1117"]).load(function(){
// $("#iframe1117").height($("#iframe1117").contents().find("body").height()+showittryitheight);
// });
// code1117 = newCode;
// }
});
$(".tryButton1117").click(function(){
$("#tryDiv1117").show();
$("#stepcodeTextarea1117").focus();
$("#stepcodeTextarea1117").height(200);
$("#iframe1117").height(0);
window.frames["iframe1117"].document.write("
");
window.frames["iframe1117"].document.write(decodeHtml($("textarea#stepcodeTextarea1117").val()));
window.frames["iframe1117"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1117"]).load(function(){
$("#iframe1117").height($("#iframe1117").contents().find("body").height()+showittryitheight);
});
$("#iframe1117").height($("#iframe1117").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1117.focus();
editor1117.setSize(null, "250");
$("#rendering1117").hide();
$("#rendered1117").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 editor1117 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1117"), {
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);
}
}
});
editor1117.on("change",function(doc){
if(!binded1117){
$(window).bind('beforeunload',function(){
binded1117 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1117 = newCode;
$("textarea#stepcodeTextarea1117").val(newCode);
if(alreadyWriteCode1117!=code1117){
lastModifedTime1117 = new Date().getTime();
$("#rendering1117").show();
$("#rendered1117").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1117 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1117.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1117").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
示例
2
:
通过function设计一个对象
顶
折
通过new Object创建对象有个问题,就是每创建一个对象,都得重新定义属性和函数。这样代码的重用性不好 那么,采用另一种方式,通过function设计一种对象。 然后实例化它 。 这种思路很像Java里的设计一种类,但是 javascript没有类,只有对象,所以我们叫设计一种对象
");
window.frames["iframe_show1148"].document.write(decodeHtml($("textarea#stepcodeTextarea1148").val()));
window.frames["iframe_show1148"].document.close();
$(window.frames["iframe_show1148"]).load(function(){
$("#iframe_show1148").height($("#iframe_show1148").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1148").height($("#iframe_show1148").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
<script>
function Hero(name){
this.name = name;
this.kill = function(){
document.write(this.name + "正在杀敌<br>");
}
}
var gareen = new Hero("盖伦");
gareen.kill();
var teemo = new Hero("提莫");
teemo.kill();
</script>
<script>
function Hero(name){
this.name = name;
this.kill = function(){
document.write(this.name + "正在杀敌<br>");
}
}
var gareen = new Hero("盖伦");
gareen.kill();
var teemo = new Hero("提莫");
teemo.kill();
</script>
");
window.frames["iframe1148"].document.write(decodeHtml(code1148));
window.frames["iframe1148"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1148"]).load(function(){
$("#iframe1148").height($("#iframe1148").contents().find("body").height()+showittryitheight);
});
$("#iframe1148").height($("#iframe1148").contents().find("body").height()+showittryitheight);
alreadyWriteCode1148 = code1148;
$("#rendering1148").hide();
$("#rendered1148").show();
}
var tRereshRetry2DemoPanel1148 = setInterval(rereshRetry2DemoPanel1148,1000);
var binded1148 = false;
$("textarea#stepcodeTextarea1148").keyup(function(){
if(!binded1148){
$(window).bind('beforeunload',function(){
binded1148 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1148 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1148!=newCode){
// window.frames["iframe1148"].document.write("
");
// window.frames["iframe1148"].document.write(decodeHtml($("textarea#stepcodeTextarea1148").val()));
// window.frames["iframe1148"].document.close();
// $(window.frames["iframe1148"]).load(function(){
// $("#iframe1148").height($("#iframe1148").contents().find("body").height()+showittryitheight);
// });
// code1148 = newCode;
// }
});
$(".tryButton1148").click(function(){
$("#tryDiv1148").show();
$("#stepcodeTextarea1148").focus();
$("#stepcodeTextarea1148").height(200);
$("#iframe1148").height(0);
window.frames["iframe1148"].document.write("
");
window.frames["iframe1148"].document.write(decodeHtml($("textarea#stepcodeTextarea1148").val()));
window.frames["iframe1148"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1148"]).load(function(){
$("#iframe1148").height($("#iframe1148").contents().find("body").height()+showittryitheight);
});
$("#iframe1148").height($("#iframe1148").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1148.focus();
editor1148.setSize(null, "250");
$("#rendering1148").hide();
$("#rendered1148").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 editor1148 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1148"), {
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);
}
}
});
editor1148.on("change",function(doc){
if(!binded1148){
$(window).bind('beforeunload',function(){
binded1148 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1148 = newCode;
$("textarea#stepcodeTextarea1148").val(newCode);
if(alreadyWriteCode1148!=code1148){
lastModifedTime1148 = new Date().getTime();
$("#rendering1148").show();
$("#rendered1148").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1148 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1148.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1148").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
示例
3
:
为已经存在的对象,增加新的方法
顶
折
现在Hero对象已经设计好了,但是我们发现需要追加一个新的方法keng(),那么就需要通过prototype实现这一点
");
window.frames["iframe_show1149"].document.write(decodeHtml($("textarea#stepcodeTextarea1149").val()));
window.frames["iframe_show1149"].document.close();
$(window.frames["iframe_show1149"]).load(function(){
$("#iframe_show1149").height($("#iframe_show1149").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1149").height($("#iframe_show1149").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
<script>
function Hero(name){
this.name = name;
this.kill = function(){
document.write(this.name + "正在杀敌<br>");
}
}
var gareen = new Hero("盖伦");
gareen.kill();
var teemo = new Hero("提莫");
teemo.kill();
Hero.prototype.keng = function(){
document.write(this.name + "正在坑队友<br>");
}
gareen.keng();
teemo.keng();
</script>
<script>
function Hero(name){
this.name = name;
this.kill = function(){
document.write(this.name + "正在杀敌<br>");
}
}
var gareen = new Hero("盖伦");
gareen.kill();
var teemo = new Hero("提莫");
teemo.kill();
Hero.prototype.keng = function(){
document.write(this.name + "正在坑队友<br>");
}
gareen.keng();
teemo.keng();
</script>
");
window.frames["iframe1149"].document.write(decodeHtml(code1149));
window.frames["iframe1149"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1149"]).load(function(){
$("#iframe1149").height($("#iframe1149").contents().find("body").height()+showittryitheight);
});
$("#iframe1149").height($("#iframe1149").contents().find("body").height()+showittryitheight);
alreadyWriteCode1149 = code1149;
$("#rendering1149").hide();
$("#rendered1149").show();
}
var tRereshRetry2DemoPanel1149 = setInterval(rereshRetry2DemoPanel1149,1000);
var binded1149 = false;
$("textarea#stepcodeTextarea1149").keyup(function(){
if(!binded1149){
$(window).bind('beforeunload',function(){
binded1149 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1149 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1149!=newCode){
// window.frames["iframe1149"].document.write("
");
// window.frames["iframe1149"].document.write(decodeHtml($("textarea#stepcodeTextarea1149").val()));
// window.frames["iframe1149"].document.close();
// $(window.frames["iframe1149"]).load(function(){
// $("#iframe1149").height($("#iframe1149").contents().find("body").height()+showittryitheight);
// });
// code1149 = newCode;
// }
});
$(".tryButton1149").click(function(){
$("#tryDiv1149").show();
$("#stepcodeTextarea1149").focus();
$("#stepcodeTextarea1149").height(200);
$("#iframe1149").height(0);
window.frames["iframe1149"].document.write("
");
window.frames["iframe1149"].document.write(decodeHtml($("textarea#stepcodeTextarea1149").val()));
window.frames["iframe1149"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1149"]).load(function(){
$("#iframe1149").height($("#iframe1149").contents().find("body").height()+showittryitheight);
});
$("#iframe1149").height($("#iframe1149").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1149.focus();
editor1149.setSize(null, "250");
$("#rendering1149").hide();
$("#rendered1149").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 editor1149 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1149"), {
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);
}
}
});
editor1149.on("change",function(doc){
if(!binded1149){
$(window).bind('beforeunload',function(){
binded1149 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1149 = newCode;
$("textarea#stepcodeTextarea1149").val(newCode);
if(alreadyWriteCode1149!=code1149){
lastModifedTime1149 = new Date().getTime();
$("#rendering1149").show();
$("#rendered1149").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1149 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1149.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1149").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
代码高亮插件双击即可选中,不过部分同学反应,通过代码高亮插件复制的代码无法在IDEA里正常显示,这里提供TEXTAREA的方式,方便复制,谢谢
1. 自行完成练习
根据练习目标尽量自己实现代码效果,期间会碰到疑问,难题,和自己不懂的地方,这些都是必要的过程
2. 带着疑问查看答案
完成过程中,碰到无法解决的问题,带着疑问,查看答案,分析答案的解决思路
3. 查看答案讲解视频
依然有不明白的地方,点开视频讲解,带着疑问,听视频讲解有问题的部分
4. 再做一遍
理解后,再从头做一遍,把有疑问的地方都捋清楚
5. 总结
最后再总结一遍,总结思路,总结解决办法,以后遇到类似的问题,怎么处理
HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。
提问之前请登陆
|