how2j.cn


工具版本兼容问题
当条件成立时,就执行相应语句

关键字 简介 示例代码
if
条件成立时执行
示例代码
else
条件不成立时执行
示例代码
else if
多条件判断 - else if
示例代码
switch
多条件判断 - switch
示例代码
练习
示例代码
示例 1 : 条件成立时执行   
示例 2 : 条件不成立时执行   
示例 3 : 多条件判断 - else if   
示例 4 : 多条件判断 - switch   
示例 5 : 练习   

示例 1 :

条件成立时执行

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 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 2 :

条件不成立时执行

else表示当条件不成立时执行的代码
"); 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>
"); 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 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 5 :

练习

练习难度
Or  姿势不对,事倍功半! 点击查看做练习的正确姿势
使用else if 实现 多条件判断-switch 中相同的效果


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


关于 前端部分-JavaScript-条件语句 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 620943819
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: http://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图