关键字
简介
示例代码
if
条件成立时执行
示例代码
else
条件不成立时执行
示例代码
else if
多条件判断 - else if
示例代码
switch
多条件判断 - switch
示例代码
练习
示例代码
示例
1
:
条件成立时执行
示例
2
:
条件不成立时执行
示例
3
:
多条件判断 - else if
示例
4
:
多条件判断 - switch
示例
5
:
练习
if条件一定是一个Boolean类型的值,当为true时,执行对应的语句
");
window.frames["iframe_show1106"].document.write(decodeHtml($("textarea#stepcodeTextarea1106").val()));
window.frames["iframe_show1106"].document.close();
$(window.frames["iframe_show1106"]).load(function(){
$("#iframe_show1106").height($("#iframe_show1106").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1106").height($("#iframe_show1106").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
运行效果
代码比较
复制代码
<script>
var age = 15;
if(age<18){
document.write("如果年纪小于18,就看 卡通");
}
</script>
<script>
var age = 15;
if(age<18){
document.write("如果年纪小于18,就看 卡通");
}
</script>
");
window.frames["iframe1106"].document.write(decodeHtml(code1106));
window.frames["iframe1106"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1106"]).load(function(){
$("#iframe1106").height($("#iframe1106").contents().find("body").height()+showittryitheight);
});
$("#iframe1106").height($("#iframe1106").contents().find("body").height()+showittryitheight);
alreadyWriteCode1106 = code1106;
$("#rendering1106").hide();
$("#rendered1106").show();
}
var tRereshRetry2DemoPanel1106 = setInterval(rereshRetry2DemoPanel1106,1000);
var binded1106 = false;
$("textarea#stepcodeTextarea1106").keyup(function(){
if(!binded1106){
$(window).bind('beforeunload',function(){
binded1106 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1106 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1106!=newCode){
// window.frames["iframe1106"].document.write("
");
// window.frames["iframe1106"].document.write(decodeHtml($("textarea#stepcodeTextarea1106").val()));
// window.frames["iframe1106"].document.close();
// $(window.frames["iframe1106"]).load(function(){
// $("#iframe1106").height($("#iframe1106").contents().find("body").height()+showittryitheight);
// });
// code1106 = newCode;
// }
});
$(".tryButton1106").click(function(){
$("#tryDiv1106").show();
$("#stepcodeTextarea1106").focus();
$("#stepcodeTextarea1106").height(200);
$("#iframe1106").height(0);
window.frames["iframe1106"].document.write("
");
window.frames["iframe1106"].document.write(decodeHtml($("textarea#stepcodeTextarea1106").val()));
window.frames["iframe1106"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1106"]).load(function(){
$("#iframe1106").height($("#iframe1106").contents().find("body").height()+showittryitheight);
});
$("#iframe1106").height($("#iframe1106").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1106.focus();
editor1106.setSize(null, "250");
$("#rendering1106").hide();
$("#rendered1106").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 editor1106 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1106"), {
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);
}
}
});
editor1106.on("change",function(doc){
if(!binded1106){
$(window).bind('beforeunload',function(){
binded1106 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1106 = newCode;
$("textarea#stepcodeTextarea1106").val(newCode);
if(alreadyWriteCode1106!=code1106){
lastModifedTime1106 = new Date().getTime();
$("#rendering1106").show();
$("#rendered1106").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1106 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1106.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1106").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
试一下
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
渲染中
渲染完成
");
window.frames["iframe_show1107"].document.write(decodeHtml($("textarea#stepcodeTextarea1107").val()));
window.frames["iframe_show1107"].document.close();
$(window.frames["iframe_show1107"]).load(function(){
$("#iframe_show1107").height($("#iframe_show1107").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1107").height($("#iframe_show1107").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
运行效果
代码比较
复制代码
<script>
var age = 22;
if(age<18){
document.write("年纪小于18就看卡通");
}
else{
document.write("否则就看 你懂的");
}
</script>
<script>
var age = 22;
if(age<18){
document.write("年纪小于18就看卡通");
}
else{
document.write("否则就看 你懂的");
}
</script>
");
window.frames["iframe1107"].document.write(decodeHtml(code1107));
window.frames["iframe1107"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1107"]).load(function(){
$("#iframe1107").height($("#iframe1107").contents().find("body").height()+showittryitheight);
});
$("#iframe1107").height($("#iframe1107").contents().find("body").height()+showittryitheight);
alreadyWriteCode1107 = code1107;
$("#rendering1107").hide();
$("#rendered1107").show();
}
var tRereshRetry2DemoPanel1107 = setInterval(rereshRetry2DemoPanel1107,1000);
var binded1107 = false;
$("textarea#stepcodeTextarea1107").keyup(function(){
if(!binded1107){
$(window).bind('beforeunload',function(){
binded1107 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1107 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1107!=newCode){
// window.frames["iframe1107"].document.write("
");
// window.frames["iframe1107"].document.write(decodeHtml($("textarea#stepcodeTextarea1107").val()));
// window.frames["iframe1107"].document.close();
// $(window.frames["iframe1107"]).load(function(){
// $("#iframe1107").height($("#iframe1107").contents().find("body").height()+showittryitheight);
// });
// code1107 = newCode;
// }
});
$(".tryButton1107").click(function(){
$("#tryDiv1107").show();
$("#stepcodeTextarea1107").focus();
$("#stepcodeTextarea1107").height(200);
$("#iframe1107").height(0);
window.frames["iframe1107"].document.write("
");
window.frames["iframe1107"].document.write(decodeHtml($("textarea#stepcodeTextarea1107").val()));
window.frames["iframe1107"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1107"]).load(function(){
$("#iframe1107").height($("#iframe1107").contents().find("body").height()+showittryitheight);
});
$("#iframe1107").height($("#iframe1107").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1107.focus();
editor1107.setSize(null, "250");
$("#rendering1107").hide();
$("#rendered1107").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 editor1107 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1107"), {
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);
}
}
});
editor1107.on("change",function(doc){
if(!binded1107){
$(window).bind('beforeunload',function(){
binded1107 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1107 = newCode;
$("textarea#stepcodeTextarea1107").val(newCode);
if(alreadyWriteCode1107!=code1107){
lastModifedTime1107 = new Date().getTime();
$("#rendering1107").show();
$("#rendered1107").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1107 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1107.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1107").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
试一下
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
渲染中
渲染完成
示例
3
:
多条件判断 - else if
顶
折
else if 用于多条件判断 在本例中 小于 18是一个条件 19-21 是一个条件 大于21是一个条件
");
window.frames["iframe_show1108"].document.write(decodeHtml($("textarea#stepcodeTextarea1108").val()));
window.frames["iframe_show1108"].document.close();
$(window.frames["iframe_show1108"]).load(function(){
$("#iframe_show1108").height($("#iframe_show1108").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1108").height($("#iframe_show1108").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
运行效果
代码比较
复制代码
<script>
var age = 20;
if(age<18){
document.write("小于18就看 卡通");
}
else if(age<22){
document.write("大于等于18,并且小于22,就看 你懂的");
}
else{
document.write("大于等于22就看 新闻联播");
}
</script>
<script>
var age = 20;
if(age<18){
document.write("小于18就看 卡通");
}
else if(age<22){
document.write("大于等于18,并且小于22,就看 你懂的");
}
else{
document.write("大于等于22就看 新闻联播");
}
</script>
");
window.frames["iframe1108"].document.write(decodeHtml(code1108));
window.frames["iframe1108"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1108"]).load(function(){
$("#iframe1108").height($("#iframe1108").contents().find("body").height()+showittryitheight);
});
$("#iframe1108").height($("#iframe1108").contents().find("body").height()+showittryitheight);
alreadyWriteCode1108 = code1108;
$("#rendering1108").hide();
$("#rendered1108").show();
}
var tRereshRetry2DemoPanel1108 = setInterval(rereshRetry2DemoPanel1108,1000);
var binded1108 = false;
$("textarea#stepcodeTextarea1108").keyup(function(){
if(!binded1108){
$(window).bind('beforeunload',function(){
binded1108 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1108 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1108!=newCode){
// window.frames["iframe1108"].document.write("
");
// window.frames["iframe1108"].document.write(decodeHtml($("textarea#stepcodeTextarea1108").val()));
// window.frames["iframe1108"].document.close();
// $(window.frames["iframe1108"]).load(function(){
// $("#iframe1108").height($("#iframe1108").contents().find("body").height()+showittryitheight);
// });
// code1108 = newCode;
// }
});
$(".tryButton1108").click(function(){
$("#tryDiv1108").show();
$("#stepcodeTextarea1108").focus();
$("#stepcodeTextarea1108").height(200);
$("#iframe1108").height(0);
window.frames["iframe1108"].document.write("
");
window.frames["iframe1108"].document.write(decodeHtml($("textarea#stepcodeTextarea1108").val()));
window.frames["iframe1108"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1108"]).load(function(){
$("#iframe1108").height($("#iframe1108").contents().find("body").height()+showittryitheight);
});
$("#iframe1108").height($("#iframe1108").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1108.focus();
editor1108.setSize(null, "250");
$("#rendering1108").hide();
$("#rendered1108").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 editor1108 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1108"), {
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);
}
}
});
editor1108.on("change",function(doc){
if(!binded1108){
$(window).bind('beforeunload',function(){
binded1108 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1108 = newCode;
$("textarea#stepcodeTextarea1108").val(newCode);
if(alreadyWriteCode1108!=code1108){
lastModifedTime1108 = new Date().getTime();
$("#rendering1108").show();
$("#rendered1108").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1108 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1108.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1108").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
试一下
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
渲染中
渲染完成
示例
4
:
多条件判断 - switch
顶
折
switch 语句与else if一样,也是进行多条件判断的
需要注意的是,每个判断结束,都要加上
break; 本例用到了Date对象,更多的用法,请参考
javascript 日期对象
");
window.frames["iframe_show1109"].document.write(decodeHtml($("textarea#stepcodeTextarea1109").val()));
window.frames["iframe_show1109"].document.close();
$(window.frames["iframe_show1109"]).load(function(){
$("#iframe_show1109").height($("#iframe_show1109").contents().find("body").height()+showittryitheight);
});
$("#iframe_show1109").height($("#iframe_show1109").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
运行效果
代码比较
复制代码
<script>
var day=new Date().getDay(); //通过日期对象获取数字形式的星期几
var today;
switch (day)
{
case 0:
today="星期天";
break;
case 1:
today="星期一";
break;
case 2:
today="星期二";
break;
case 3:
today="星期三";
break;
case 4:
today="星期四";
break;
case 5:
today="星期五";
break;
case 6:
today="星期六";
break;
}
document.write("今天是 : "+today);
</script>
<script>
var day=new Date().getDay(); //通过日期对象获取数字形式的星期几
var today;
switch (day)
{
case 0:
today="星期天";
break;
case 1:
today="星期一";
break;
case 2:
today="星期二";
break;
case 3:
today="星期三";
break;
case 4:
today="星期四";
break;
case 5:
today="星期五";
break;
case 6:
today="星期六";
break;
}
document.write("今天是 : "+today);
</script>
");
window.frames["iframe1109"].document.write(decodeHtml(code1109));
window.frames["iframe1109"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1109"]).load(function(){
$("#iframe1109").height($("#iframe1109").contents().find("body").height()+showittryitheight);
});
$("#iframe1109").height($("#iframe1109").contents().find("body").height()+showittryitheight);
alreadyWriteCode1109 = code1109;
$("#rendering1109").hide();
$("#rendered1109").show();
}
var tRereshRetry2DemoPanel1109 = setInterval(rereshRetry2DemoPanel1109,1000);
var binded1109 = false;
$("textarea#stepcodeTextarea1109").keyup(function(){
if(!binded1109){
$(window).bind('beforeunload',function(){
binded1109 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code1109 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code1109!=newCode){
// window.frames["iframe1109"].document.write("
");
// window.frames["iframe1109"].document.write(decodeHtml($("textarea#stepcodeTextarea1109").val()));
// window.frames["iframe1109"].document.close();
// $(window.frames["iframe1109"]).load(function(){
// $("#iframe1109").height($("#iframe1109").contents().find("body").height()+showittryitheight);
// });
// code1109 = newCode;
// }
});
$(".tryButton1109").click(function(){
$("#tryDiv1109").show();
$("#stepcodeTextarea1109").focus();
$("#stepcodeTextarea1109").height(200);
$("#iframe1109").height(0);
window.frames["iframe1109"].document.write("
");
window.frames["iframe1109"].document.write(decodeHtml($("textarea#stepcodeTextarea1109").val()));
window.frames["iframe1109"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe1109"]).load(function(){
$("#iframe1109").height($("#iframe1109").contents().find("body").height()+showittryitheight);
});
$("#iframe1109").height($("#iframe1109").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor1109.focus();
editor1109.setSize(null, "250");
$("#rendering1109").hide();
$("#rendered1109").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 editor1109 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1109"), {
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);
}
}
});
editor1109.on("change",function(doc){
if(!binded1109){
$(window).bind('beforeunload',function(){
binded1109 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code1109 = newCode;
$("textarea#stepcodeTextarea1109").val(newCode);
if(alreadyWriteCode1109!=code1109){
lastModifedTime1109 = new Date().getTime();
$("#rendering1109").show();
$("#rendered1109").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor1109 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor1109.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv1109").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
试一下
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
渲染中
渲染完成
代码高亮插件双击即可选中,不过部分同学反应,通过代码高亮插件复制的代码无法在IDEA里正常显示,这里提供TEXTAREA的方式,方便复制,谢谢
1. 自行完成练习
根据练习目标尽量自己实现代码效果,期间会碰到疑问,难题,和自己不懂的地方,这些都是必要的过程
2. 带着疑问查看答案
完成过程中,碰到无法解决的问题,带着疑问,查看答案,分析答案的解决思路
3. 查看答案讲解视频
依然有不明白的地方,点开视频讲解,带着疑问,听视频讲解有问题的部分
4. 再做一遍
理解后,再从头做一遍,把有疑问的地方都捋清楚
5. 总结
最后再总结一遍,总结思路,总结解决办法,以后遇到类似的问题,怎么处理
HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。
提问之前请登陆