vue實(shí)現(xiàn)驗(yàn)證碼按鈕倒計(jì)時(shí)功能
本人最近開始嘗試學(xué)習(xí)vue.js。想使用vue寫一個(gè)小例子,就選擇做驗(yàn)證碼按鈕倒計(jì)時(shí)功能。
上網(wǎng)上搜了一下,也把他們的代碼試了一下,自己出了很多問(wèn)題。所以,需要寫一篇基礎(chǔ)入門的文章,避免后面人采坑。
這是按照網(wǎng)上寫的HTML頁(yè)面
<div class="register-pannel" id ="register-pannel"> <div class="register-l" align="center"> <div class="input-group" style="width: 300px;"> <input type="text" class="form-control" placeholder="郵箱/手機(jī)號(hào)/用戶名" style="height: 40px;" /> <span class="glyphicon glyphicon-user form-control-feedback" aria-hidden="true" /> </div> <br /> <div class="input-group" style="width: 300px;"> <input type="text" class="form-control" placeholder="密碼" style="height: 40px;" /> <span class="glyphicon glyphicon-lock form-control-feedback" /> </div> <br /> <div class="input-group" style="width: 300px;"> <input type="text" class="form-control" placeholder="手機(jī)號(hào)" style="height: 40px;" /> <span class="glyphicon glyphicon-phone form-control-feedback" /> </div> <br /> <div class="input-group" style="width: 300px;"> <span class="register-msg-btn" v-show="show" v-on:click="getCode">發(fā)送驗(yàn)證碼</span> <span class="register-msg-btn" v-show="!show">{{count}} s</span> <input type="text" class="form-control" placeholder="驗(yàn)證碼" style="float: right; height: 40px; width: 150px;" /> <span class="glyphicon glyphicon-font form-control-feedback" /> </div> <br /> <span class="btn-register">注冊(cè)</span> </div>
js寫成
<script> <span style="white-space: pre;"> </span>data(){ <span style="white-space: pre;"> </span>return { <span style="white-space: pre;"> </span>show: true, <span style="white-space: pre;"> </span>count: '', <span style="white-space: pre;"> </span>timer: null, <span style="white-space: pre;"> </span>} <span style="white-space: pre;"> </span>}, <span style="white-space: pre;"> </span>methods:{ <span style="white-space: pre;"> </span>getCode(){ <span style="white-space: pre;"> </span>const TIME_COUNT = 60; <span style="white-space: pre;"> </span>if (!this.timer) { <span style="white-space: pre;"> </span>this.count = TIME_COUNT; <span style="white-space: pre;"> </span>this.show = false; <span style="white-space: pre;"> </span>this.timer = setInterval(() => { <span style="white-space: pre;"> </span>if (this.count > 0 && this.count <= TIME_COUNT) { <span style="white-space: pre;"> </span>this.count--; <span style="white-space: pre;"> </span>} else { <span style="white-space: pre;"> </span>this.show = true; <span style="white-space: pre;"> </span>clearInterval(this.timer); <span style="white-space: pre;"> </span>this.timer = null; <span style="white-space: pre;"> </span>} <span style="white-space: pre;"> </span>}, 1000) <span style="white-space: pre;"> </span>} <span style="white-space: pre;"> </span>} <span style="white-space: pre;"> </span>} </script>
發(fā)現(xiàn)瀏覽器一直報(bào)錯(cuò)Uncaught SyntaxError: Unexpected token {
所以按照官方文檔的格式,把js的結(jié)構(gòu)改成
<script> new Vue({ el:'.register-pannel', data:{ show:true, timer:null, count:'' }, methods:{ getCode(){ this.show = false; const TIME_COUNT = 60; if (!this.timer) { this.count = TIME_COUNT; this.show = false; this.timer = setInterval(() => { if (this.count > 0 && this.count <= TIME_COUNT) { this.count--; } else { this.show = true; clearInterval(this.timer); this.timer = null; } }, 1000) } } } }); </script>
于是格式是沒有問(wèn)題了,但是樣式并沒有生效。變成了另一個(gè)樣子。
上網(wǎng)上搜了很多。
有說(shuō)是js引用順序的問(wèn)題。
有說(shuō)是將js寫進(jìn)window.onload
的。試了一下,發(fā)現(xiàn)都不對(duì)。
后來(lái),在官方文檔中發(fā)現(xiàn)了el屬性:為實(shí)例提供掛載元素。值可以是 CSS 選擇符,或?qū)嶋H HTML 元素,或返回 HTML 元素的函數(shù)。
所以改成
<script> new Vue({ el:'.register-pannel', //注冊(cè)div的class data:{ show:true, timer:null, count:'' }, methods:{ getCode(){ this.show = false; const TIME_COUNT = 60; if (!this.timer) { this.count = TIME_COUNT; this.show = false; this.timer = setInterval(() => { if (this.count > 0 && this.count <= TIME_COUNT) { this.count--; } else { this.show = true; clearInterval(this.timer); this.timer = null; } }, 1000) } } } }); </script>
效果就出來(lái)了。
總結(jié)
以上所述是小編給大家介紹vue實(shí)現(xiàn)驗(yàn)證碼按鈕倒計(jì)時(shí)功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- vue+vant實(shí)現(xiàn)商品列表批量倒計(jì)時(shí)功能
- Vue寫一個(gè)簡(jiǎn)單的倒計(jì)時(shí)按鈕功能
- 簡(jiǎn)單實(shí)現(xiàn)vue驗(yàn)證碼60秒倒計(jì)時(shí)功能
- vue2.0實(shí)現(xiàn)倒計(jì)時(shí)的插件(時(shí)間戳 刷新 跳轉(zhuǎn) 都不影響)
- vue實(shí)現(xiàn)時(shí)間倒計(jì)時(shí)功能
- 基于vue的短信驗(yàn)證碼倒計(jì)時(shí)demo
- vue實(shí)現(xiàn)商城秒殺倒計(jì)時(shí)功能
- vue實(shí)現(xiàn)倒計(jì)時(shí)獲取驗(yàn)證碼效果
- vue中多個(gè)倒計(jì)時(shí)實(shí)現(xiàn)代碼實(shí)例
- vue實(shí)現(xiàn)列表倒計(jì)時(shí)
相關(guān)文章
Vue?基于?vuedraggable?實(shí)現(xiàn)選中、拖拽、排序效果
這篇文章主要介紹了Vue?基于?vuedraggable?實(shí)現(xiàn)選中、拖拽、排序效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05輕量級(jí)富文本編輯器wangEditor結(jié)合vue使用方法示例
在我們項(xiàng)目中,有些時(shí)候需要使用富文本編輯器。本文將以百度開發(fā)的Ueditor結(jié)合Vue.js介紹一下。非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-10-10Vue表格首列相同數(shù)據(jù)合并實(shí)現(xiàn)方法
這篇文章主要介紹了Vue實(shí)現(xiàn)表格首列相同數(shù)據(jù)合并的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04基于axios請(qǐng)求封裝的vue應(yīng)用實(shí)例代碼
這篇文章主要給大家介紹了基于axios請(qǐng)求封裝的vue應(yīng)用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05解決在vue項(xiàng)目中,發(fā)版之后,背景圖片報(bào)錯(cuò),路徑不對(duì)的問(wèn)題
下面小編就為大家分享一篇解決在vue項(xiàng)目中,發(fā)版之后,背景圖片報(bào)錯(cuò),路徑不對(duì)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03webstorm和.vue中es6語(yǔ)法報(bào)錯(cuò)的解決方法
本篇文章主要介紹了webstorm和.vue中es6語(yǔ)法報(bào)錯(cuò)的解決方法,小編總結(jié)了webstorm和.vue中出現(xiàn)的es6語(yǔ)法報(bào)錯(cuò),避免大家采坑,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Vue?設(shè)置圖片不轉(zhuǎn)為base64的方式
這篇文章主要介紹了Vue實(shí)現(xiàn)設(shè)置圖片不轉(zhuǎn)為base64的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02