代碼實例:
代碼描述:點擊切換按鈕,來改變顯示的內容,切換不同的單位。flag相當于一個開關,控制開關的改變,來切換不同的單位。同樣適用于其他的切換內容實例,也可設置按鈕點擊顯示隱藏等。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>vue點擊切換改變內容</title> <script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script> </head> <body> <Col span="2" style="text-align: center;"> <p style='margin-top:8px;font-size:12px;' v-text="btnText" v-show='flag==true'></p> <p style='margin-top:8px;font-size:12px;' v-text="btnText" v-show='flag==false'></p> </Col> <Col span='2'> <span @click='switchChange'> <Icon type="arrow-swap" class='contractadd_icon'></Icon> </span> </Col> <script type="text/javascript"> new Vue({ el:"#example", data:{ flag:true,//單位切換開關 btnText:'元/噸', }, methods:{ showToggle:function(){ this.flag = !this.flag if(this.flag==true){ this.btnText = "元/噸" }else if(this.flag==false){ this.btnText = "元/方" } } } }) </script> </body> </html> 拓展知識:vue 點擊按鈕改變頁面顯示內容的方法
如下所示:
<!DOCTYPE html><html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-on="http://www.w3.org/1999/xhtml"><head> <meta charset="UTF-8"> <title>Title</title> <script src="https://cdn.jsdelivr.net/npm/axios@0.12.0/dist/axios.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/lodash@4.13.1/lodash.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/vue"></script></head><body><div id="one"> <template v-if="loginType === 'username'"> <label>Username</label> <input placeholder="Enter your username" key="username-input"> </template> <template v-else> <label>Email</label> <input placeholder="Enter your email address" key="email-input"> </template> <button @click="change">change</button></div> <script type="text/javascript"> var vm = new Vue({ el:'#one', data:{ loginType : 'username' }, methods:{ change:function(){ if(this.loginType=='username'){ this.loginType='email' } else{ this.loginType='username' } } } })</script> </body></html>以上這篇Vue.js點擊切換按鈕改變內容的實例講解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。
新聞熱點
疑難解答