how2j.cn

步骤 1 : v-if   
步骤 2 : v-else   
步骤 3 : v-else-if   

步骤 1 :

v-if

通过toggle函数切换show的值。 通过v-if 语句,当show 是true的时候,显示当前元素

<div v-if="show"> 默认这一条是看得见的</div>
"); window.frames["iframe_show7795"].document.write(decodeHtml($("textarea#stepcodeTextarea7795").val())); window.frames["iframe_show7795"].document.close(); $(window.frames["iframe_show7795"]).load(function(){ $("#iframe_show7795").height($("#iframe_show7795").contents().find("body").height()+showittryitheight); }); $("#iframe_show7795").height($("#iframe_show7795").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/study/vue.min.js"></script> <div id="div1"> <button v-on:click="toggle">切换隐藏显示</button> <div v-if="show"> 默认这一条是看得见的</div> </div> <script> new Vue({ el: '#div1', data: { show:true }, methods:{ toggle: function(){ this.show=!this.show; } } }) </script>
"); window.frames["iframe7795"].document.write(decodeHtml(code7795)); window.frames["iframe7795"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7795"]).load(function(){ $("#iframe7795").height($("#iframe7795").contents().find("body").height()+showittryitheight); }); $("#iframe7795").height($("#iframe7795").contents().find("body").height()+showittryitheight); alreadyWriteCode7795 = code7795; $("#rendering7795").hide(); $("#rendered7795").show(); } var tRereshRetry2DemoPanel7795 = setInterval(rereshRetry2DemoPanel7795,1000); var binded7795 = false; $("textarea#stepcodeTextarea7795").keyup(function(){ if(!binded7795){ $(window).bind('beforeunload',function(){ binded7795 = true; return "xxxx"; }); } var newCode = $(this).val() code7795 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code7795!=newCode){ // window.frames["iframe7795"].document.write("
"); // window.frames["iframe7795"].document.write(decodeHtml($("textarea#stepcodeTextarea7795").val())); // window.frames["iframe7795"].document.close(); // $(window.frames["iframe7795"]).load(function(){ // $("#iframe7795").height($("#iframe7795").contents().find("body").height()+showittryitheight); // }); // code7795 = newCode; // } }); $(".tryButton7795").click(function(){ $("#tryDiv7795").show(); $("#stepcodeTextarea7795").focus(); $("#stepcodeTextarea7795").height(200); $("#iframe7795").height(0); window.frames["iframe7795"].document.write("
"); window.frames["iframe7795"].document.write(decodeHtml($("textarea#stepcodeTextarea7795").val())); window.frames["iframe7795"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7795"]).load(function(){ $("#iframe7795").height($("#iframe7795").contents().find("body").height()+showittryitheight); }); $("#iframe7795").height($("#iframe7795").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor7795.focus(); editor7795.setSize(null, "250"); $("#rendering7795").hide(); $("#rendered7795").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 editor7795 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea7795"), { 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); } } }); editor7795.on("change",function(doc){ if(!binded7795){ $(window).bind('beforeunload',function(){ binded7795 = true; return "xxxx"; }); } var newCode = doc.getValue(); code7795 = newCode; $("textarea#stepcodeTextarea7795").val(newCode); if(alreadyWriteCode7795!=code7795){ lastModifedTime7795 = new Date().getTime(); $("#rendering7795").show(); $("#rendered7795").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor7795 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor7795.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv7795").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
步骤 2 :

v-else

v-else 用法,如代码所示,没什么好说的。。。
"); window.frames["iframe_show7796"].document.write(decodeHtml($("textarea#stepcodeTextarea7796").val())); window.frames["iframe_show7796"].document.close(); $(window.frames["iframe_show7796"]).load(function(){ $("#iframe_show7796").height($("#iframe_show7796").contents().find("body").height()+showittryitheight); }); $("#iframe_show7796").height($("#iframe_show7796").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/study/vue.min.js"></script> <div id="div1"> <button v-on:click="moyiba"> 摸一把彩票 10%的几率,建议一边点击一边心里默数,多少次了,站长表示最多点了40次才中奖,妈蛋~ </button> <div v-if="show"> 中了500万!</div> <div v-else>谢谢惠顾!</div> </div> <script> new Vue({ el: '#div1', data: { show:false }, methods:{ moyiba: function(){ this.show=Math.random()<0.1 } } }) </script>
"); window.frames["iframe7796"].document.write(decodeHtml(code7796)); window.frames["iframe7796"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7796"]).load(function(){ $("#iframe7796").height($("#iframe7796").contents().find("body").height()+showittryitheight); }); $("#iframe7796").height($("#iframe7796").contents().find("body").height()+showittryitheight); alreadyWriteCode7796 = code7796; $("#rendering7796").hide(); $("#rendered7796").show(); } var tRereshRetry2DemoPanel7796 = setInterval(rereshRetry2DemoPanel7796,1000); var binded7796 = false; $("textarea#stepcodeTextarea7796").keyup(function(){ if(!binded7796){ $(window).bind('beforeunload',function(){ binded7796 = true; return "xxxx"; }); } var newCode = $(this).val() code7796 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code7796!=newCode){ // window.frames["iframe7796"].document.write("
"); // window.frames["iframe7796"].document.write(decodeHtml($("textarea#stepcodeTextarea7796").val())); // window.frames["iframe7796"].document.close(); // $(window.frames["iframe7796"]).load(function(){ // $("#iframe7796").height($("#iframe7796").contents().find("body").height()+showittryitheight); // }); // code7796 = newCode; // } }); $(".tryButton7796").click(function(){ $("#tryDiv7796").show(); $("#stepcodeTextarea7796").focus(); $("#stepcodeTextarea7796").height(200); $("#iframe7796").height(0); window.frames["iframe7796"].document.write("
"); window.frames["iframe7796"].document.write(decodeHtml($("textarea#stepcodeTextarea7796").val())); window.frames["iframe7796"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7796"]).load(function(){ $("#iframe7796").height($("#iframe7796").contents().find("body").height()+showittryitheight); }); $("#iframe7796").height($("#iframe7796").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor7796.focus(); editor7796.setSize(null, "250"); $("#rendering7796").hide(); $("#rendered7796").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 editor7796 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea7796"), { 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); } } }); editor7796.on("change",function(doc){ if(!binded7796){ $(window).bind('beforeunload',function(){ binded7796 = true; return "xxxx"; }); } var newCode = doc.getValue(); code7796 = newCode; $("textarea#stepcodeTextarea7796").val(newCode); if(alreadyWriteCode7796!=code7796){ lastModifedTime7796 = new Date().getTime(); $("#rendering7796").show(); $("#rendered7796").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor7796 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor7796.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv7796").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
步骤 3 :

v-else-if

v-else-if 用法
"); window.frames["iframe_show7797"].document.write(decodeHtml($("textarea#stepcodeTextarea7797").val())); window.frames["iframe_show7797"].document.close(); $(window.frames["iframe_show7797"]).load(function(){ $("#iframe_show7797").height($("#iframe_show7797").contents().find("body").height()+showittryitheight); }); $("#iframe_show7797").height($("#iframe_show7797").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script src="http://127.0.0.1/study/vue.min.js"></script> <div id="div1"> <button v-on:click="toutai"> 看看下辈子投胎是做什么的 </button> <div v-if="number>98"> 神仙</div> <div v-else-if="number>95"> 国家领导人</div> <div v-else-if="number>90"> 大富商</div> <div v-else-if="number>80"> 大企业主</div> <div v-else-if="number>70"> 演艺明星</div> <div v-else-if="number>60"> 小企业主</div> <div v-else-if="number>50"> 普通公务员</div> <div v-else-if="number>40"> 小个体户</div> <div v-else-if="number>30"> 血汗工厂工人</div> <div v-else-if="number>20"> 偏远山区农民</div> <div v-else> 流浪汉</div> </div> <script> new Vue({ el: '#div1', data: { number:0 }, methods:{ toutai: function(){ this.number=Math.random()*100 } } }) </script>
"); window.frames["iframe7797"].document.write(decodeHtml(code7797)); window.frames["iframe7797"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7797"]).load(function(){ $("#iframe7797").height($("#iframe7797").contents().find("body").height()+showittryitheight); }); $("#iframe7797").height($("#iframe7797").contents().find("body").height()+showittryitheight); alreadyWriteCode7797 = code7797; $("#rendering7797").hide(); $("#rendered7797").show(); } var tRereshRetry2DemoPanel7797 = setInterval(rereshRetry2DemoPanel7797,1000); var binded7797 = false; $("textarea#stepcodeTextarea7797").keyup(function(){ if(!binded7797){ $(window).bind('beforeunload',function(){ binded7797 = true; return "xxxx"; }); } var newCode = $(this).val() code7797 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code7797!=newCode){ // window.frames["iframe7797"].document.write("
"); // window.frames["iframe7797"].document.write(decodeHtml($("textarea#stepcodeTextarea7797").val())); // window.frames["iframe7797"].document.close(); // $(window.frames["iframe7797"]).load(function(){ // $("#iframe7797").height($("#iframe7797").contents().find("body").height()+showittryitheight); // }); // code7797 = newCode; // } }); $(".tryButton7797").click(function(){ $("#tryDiv7797").show(); $("#stepcodeTextarea7797").focus(); $("#stepcodeTextarea7797").height(200); $("#iframe7797").height(0); window.frames["iframe7797"].document.write("
"); window.frames["iframe7797"].document.write(decodeHtml($("textarea#stepcodeTextarea7797").val())); window.frames["iframe7797"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe7797"]).load(function(){ $("#iframe7797").height($("#iframe7797").contents().find("body").height()+showittryitheight); }); $("#iframe7797").height($("#iframe7797").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor7797.focus(); editor7797.setSize(null, "250"); $("#rendering7797").hide(); $("#rendered7797").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 editor7797 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea7797"), { 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); } } }); editor7797.on("change",function(doc){ if(!binded7797){ $(window).bind('beforeunload',function(){ binded7797 = true; return "xxxx"; }); } var newCode = doc.getValue(); code7797 = newCode; $("textarea#stepcodeTextarea7797").val(newCode); if(alreadyWriteCode7797!=code7797){ lastModifedTime7797 = new Date().getTime(); $("#rendering7797").show(); $("#rendered7797").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor7797 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor7797.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv7797").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果


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


关于 前端部分-Vue.js-条件语句 的提问

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

上传截图