|
工具版本兼容问题
JavaScript提供了一种try catch的错误处理机制,当有错误抛出的时候,可以catch住。
示例
1
:
调用不存在的函数
示例
2
:
try catch
在本例中故意调用不存在的函数f2, 因为函数f2不存在,所以会出错,并且会中断执行。 导致后面的代码也不会执行了。
");
window.frames["iframe_show1114"].document.write(decodeHtml($("textarea#stepcodeTextarea1114").val()));
window.frames["iframe_show1114"].document.close();
$(window.frames["iframe_show1114"]).load(function(){
$("#iframe_show1114").height($("#iframe_show1114").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1114").height($("#iframe_show1114").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
<script>
document.write("试图调用不存在的函数f2()就会报错,并且导致javascript中止执行,后面的代码也不会执行了");
function f1(){
//函数f1是存在的
}
f2(); //调用不存在的函数f2();
document.write("剩下的代码");
</script>
<script>
document.write("试图调用不存在的函数f2()就会报错,并且导致javascript中止执行,后面的代码也不会执行了");
function f1(){
//函数f1是存在的
}
f2(); //调用不存在的函数f2();
document.write("剩下的代码");
</script>
");
window.frames["iframe1114"].document.write(decodeHtml(code1114));
window.frames["iframe1114"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1114"]).load(function(){
$("#iframe1114").height($("#iframe1114").contents().find("body").height()+showittryitheight);
});
$("#iframe1114").height($("#iframe1114").contents().find("body").height()+showittryitheight);
alreadyWriteCode1114 = code1114;
$("#rendering1114").hide();
$("#rendered1114").show();
}
var tRereshRetry2DemoPanel1114 = setInterval(rereshRetry2DemoPanel1114,1000);
var binded1114 = false;
$("textarea#stepcodeTextarea1114").keyup(function(){
if(!binded1114){
$(window).bind('beforeunload',function(){
binded1114 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1114 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1114!=newCode){
// window.frames["iframe1114"].document.write("
");
// window.frames["iframe1114"].document.write(decodeHtml($("textarea#stepcodeTextarea1114").val()));
// window.frames["iframe1114"].document.close();
// $(window.frames["iframe1114"]).load(function(){
// $("#iframe1114").height($("#iframe1114").contents().find("body").height()+showittryitheight);
// });
// code1114 = newCode;
// }
});
$(".tryButton1114").click(function(){
$("#tryDiv1114").show();
$("#stepcodeTextarea1114").focus();
$("#stepcodeTextarea1114").height(200);
$("#iframe1114").height(0);
window.frames["iframe1114"].document.write("
");
window.frames["iframe1114"].document.write(decodeHtml($("textarea#stepcodeTextarea1114").val()));
window.frames["iframe1114"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1114"]).load(function(){
$("#iframe1114").height($("#iframe1114").contents().find("body").height()+showittryitheight);
});
$("#iframe1114").height($("#iframe1114").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1114.focus();
editor1114.setSize(null, "250");
$("#rendering1114").hide();
$("#rendered1114").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 editor1114 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1114"), {
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);
}
}
});
editor1114.on("change",function(doc){
if(!binded1114){
$(window).bind('beforeunload',function(){
binded1114 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1114 = newCode;
$("textarea#stepcodeTextarea1114").val(newCode);
if(alreadyWriteCode1114!=code1114){
lastModifedTime1114 = new Date().getTime();
$("#rendering1114").show();
$("#rendered1114").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1114 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1114.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1114").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
对f2()调用进行try catch的处理。 捕捉到了该方法未定义的错误。并打印出来。 后续的代码,也就可以继续执行了。
");
window.frames["iframe_show1115"].document.write(decodeHtml($("textarea#stepcodeTextarea1115").val()));
window.frames["iframe_show1115"].document.close();
$(window.frames["iframe_show1115"]).load(function(){
$("#iframe_show1115").height($("#iframe_show1115").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1115").height($("#iframe_show1115").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
<script>
function f1(){
//函数f1是存在的
}
try{
document.write("试图调用不存在的函数f2()<br>");
f2(); //调用不存在的函数f2();
}
catch(err){
document.write("捕捉到错误产生:");
document.write(err.message);
}
document.write("<p>因为错误被捕捉了,所以后续的代码能够继续执行</p>");
</script>
<script>
function f1(){
//函数f1是存在的
}
try{
document.write("试图调用不存在的函数f2()<br>");
f2(); //调用不存在的函数f2();
}
catch(err){
document.write("捕捉到错误产生:");
document.write(err.message);
}
document.write("<p>因为错误被捕捉了,所以后续的代码能够继续执行</p>");
</script>
");
window.frames["iframe1115"].document.write(decodeHtml(code1115));
window.frames["iframe1115"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1115"]).load(function(){
$("#iframe1115").height($("#iframe1115").contents().find("body").height()+showittryitheight);
});
$("#iframe1115").height($("#iframe1115").contents().find("body").height()+showittryitheight);
alreadyWriteCode1115 = code1115;
$("#rendering1115").hide();
$("#rendered1115").show();
}
var tRereshRetry2DemoPanel1115 = setInterval(rereshRetry2DemoPanel1115,1000);
var binded1115 = false;
$("textarea#stepcodeTextarea1115").keyup(function(){
if(!binded1115){
$(window).bind('beforeunload',function(){
binded1115 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1115 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1115!=newCode){
// window.frames["iframe1115"].document.write("
");
// window.frames["iframe1115"].document.write(decodeHtml($("textarea#stepcodeTextarea1115").val()));
// window.frames["iframe1115"].document.close();
// $(window.frames["iframe1115"]).load(function(){
// $("#iframe1115").height($("#iframe1115").contents().find("body").height()+showittryitheight);
// });
// code1115 = newCode;
// }
});
$(".tryButton1115").click(function(){
$("#tryDiv1115").show();
$("#stepcodeTextarea1115").focus();
$("#stepcodeTextarea1115").height(200);
$("#iframe1115").height(0);
window.frames["iframe1115"].document.write("
");
window.frames["iframe1115"].document.write(decodeHtml($("textarea#stepcodeTextarea1115").val()));
window.frames["iframe1115"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1115"]).load(function(){
$("#iframe1115").height($("#iframe1115").contents().find("body").height()+showittryitheight);
});
$("#iframe1115").height($("#iframe1115").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1115.focus();
editor1115.setSize(null, "250");
$("#rendering1115").hide();
$("#rendered1115").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 editor1115 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1115"), {
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);
}
}
});
editor1115.on("change",function(doc){
if(!binded1115){
$(window).bind('beforeunload',function(){
binded1115 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1115 = newCode;
$("textarea#stepcodeTextarea1115").val(newCode);
if(alreadyWriteCode1115!=code1115){
lastModifedTime1115 = new Date().getTime();
$("#rendering1115").show();
$("#rendered1115").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1115 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1115.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1115").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
代码高亮插件双击即可选中,不过部分同学反应,通过代码高亮插件复制的代码无法在IDEA里正常显示,这里提供TEXTAREA的方式,方便复制,谢谢
1. 自行完成练习
根据练习目标尽量自己实现代码效果,期间会碰到疑问,难题,和自己不懂的地方,这些都是必要的过程
2. 带着疑问查看答案
完成过程中,碰到无法解决的问题,带着疑问,查看答案,分析答案的解决思路
3. 查看答案讲解视频
依然有不明白的地方,点开视频讲解,带着疑问,听视频讲解有问题的部分
4. 再做一遍
理解后,再从头做一遍,把有疑问的地方都捋清楚
5. 总结
最后再总结一遍,总结思路,总结解决办法,以后遇到类似的问题,怎么处理
HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。
提问之前请登陆
|