Vue 框架之鍵盤事件、健值修飾符、雙向數(shù)據(jù)綁定
一、鍵盤事件,當(dāng)按鍵盤時(shí),在控制臺(tái)輸出提示
html 源碼:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>VueLearn-cnblogs/xpwi</title> <!--引入自定義的樣式--> <link rel="stylesheet" href="css/style.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" /> <!--引入 vue 核心 js--> <script type="text/javascript" src="js/vue.js" ></script> </head> <body> <!--vue-app 是根容器,定義一個(gè) id,然后在 js 里操作--> <div id="vue-app"> <!--name 具體的值是在 js 中定義的--> <h2> 鍵盤 Events </h2> <label>姓名:</label> <input type="text" v-on:keyup="logName" /> <label>年齡:</label> <input type="text" v-on:keyup="logAge" /> </div> <!--引入自己的 js,注意必須寫在 body 標(biāo)簽里最后,因?yàn)楸仨毾燃虞d你的整個(gè) HTML DOM,才回去執(zhí)行 vue 實(shí)例--> <script type="text/javascript" src="js/appEvent.js" ></script> </body> </html>
js 源碼:
//實(shí)例化 vue 對(duì)象 new Vue({ //注意代碼格式 //el:element 需要獲取的元素,一定是 html 中的根容器元素 el:"#vue-app", data:{ }, methods:{ logName: function(){ console.log("你這正在輸入名字!"); }, logAge: function(){ console.log("你這正在輸入年齡!"); } } });
上面代碼是當(dāng)用戶點(diǎn)擊輸入框后,只要按下鍵盤就會(huì)在控制臺(tái)打印一次提示,實(shí)際應(yīng)用的并不多,下面介紹當(dāng)用戶按下回車鍵時(shí),才觸發(fā)
二、健值修飾符
下面在時(shí)間后面加上:.enter
就可以實(shí)現(xiàn)只監(jiān)聽 enter 鍵,就可以實(shí)現(xiàn),當(dāng)用戶在輸入完成,按下回車鍵時(shí),觸發(fā)我們自定義的事件
<input type="text" v-on:keyup.enter="logName" />
三、雙向數(shù)據(jù)綁定 input,selecet,textarea
適用:input,selecet,textarea 三種標(biāo)簽
js 文件種拿到 html 文件種輸入的數(shù)據(jù),然后 html 中拿到 js 文件中的數(shù)據(jù)
源代碼 html 文件:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>VueLearn-cnblogs/xpwi</title> <!--引入自定義的樣式--> <link rel="stylesheet" href="css/style.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" /> <!--引入 vue 核心 js--> <script type="text/javascript" src="js/vue.js" ></script> </head> <body> <!--vue-app 是根容器,定義一個(gè) id,然后在 js 里操作--> <div id="vue-app"> <!--name 具體的值是在 js 中定義的--> <h2> 鍵盤 Events </h2> <label>姓名:</label> <!--加一個(gè) ref 用來在 js 文件中拿到 input 標(biāo)簽中輸入的內(nèi)容--> <input ref="userName" type="text" /> <label>年齡:</label> <input ref="userAge" type="text" v-on:keyup.enter="notice" /> <hr /> <span>已確認(rèn)信息:</span> <br /> 姓名:{{name}}<br /> 年齡:{{age}} </div> <!--引入自己的 js,注意必須寫在 body 標(biāo)簽里最后,因?yàn)楸仨毾燃虞d你的整個(gè) HTML DOM,才回去執(zhí)行 vue 實(shí)例--> <script type="text/javascript" src="js/appEvent.js" ></script> </body> </html>
源代碼 js 文件:
//實(shí)例化 vue 對(duì)象 new Vue({ //注意代碼格式 //el:element 需要獲取的元素,一定是 html 中的根容器元素 el:"#vue-app", data:{ name : "", age : "" }, methods:{ notice: function(){ //console.log("你這正在輸入年齡!"); //this.name 是 js 文件上面定義的一個(gè) name 變量 this.name = this.$refs.userName.value; this.age = this.$refs.userAge.value; alert("姓名:" + this.name + " 年齡:" + this.age); } } });
四、雙向數(shù)據(jù)綁定 input,selecet,textarea (二)
上面數(shù)據(jù)綁定是在按下 輸入回車鍵的時(shí)候,來回的同步數(shù)據(jù),下面介紹另一種方式,實(shí)現(xiàn)雙向數(shù)據(jù)綁定
雙向數(shù)據(jù)綁定第二種方法:
源代碼 html 文件:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>VueLearn-cnblogs/xpwi</title> <!--引入自定義的樣式--> <link rel="stylesheet" href="css/style.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" /> <!--引入 vue 核心 js--> <script type="text/javascript" src="js/vue.js" ></script> </head> <body> <!--vue-app 是根容器,定義一個(gè) id,然后在 js 里操作--> <div id="vue-app"> <!--name 具體的值是在 js 中定義的--> <h2> 鍵盤 Events </h2> <label>姓名:</label> <!--加一個(gè) ref 用來在 js 文件中拿到 input 標(biāo)簽中輸入的內(nèi)容--> <input ref="userName" type="text" v-model="name" /> <label>年齡:</label> <input ref="userAge" type="text" v-model="age"/> <hr /> <span>同步數(shù)據(jù):</span> <br /> 姓名:{{name}}<br /> 年齡:{{age}} </div> <!--引入自己的 js,注意必須寫在 body 標(biāo)簽里最后,因?yàn)楸仨毾燃虞d你的整個(gè) HTML DOM,才回去執(zhí)行 vue 實(shí)例--> <script type="text/javascript" src="js/appEvent.js" ></script> </body> </html>
源代碼 js 文件:
//實(shí)例化 vue 對(duì)象 new Vue({ //注意代碼格式 //el:element 需要獲取的元素,一定是 html 中的根容器元素 el:"#vue-app", data:{ name : "", age : "" }, methods:{ notice: function(){ //console.log("你這正在輸入年齡!"); //this.name 是 js 文件上面定義的一個(gè) name 變量 this.name = this.$refs.userName.value; this.age = this.$refs.userAge.value; alert("姓名:" + this.name + " 年齡:" + this.age); } } });
總結(jié)
以上所述是小編給大家介紹的Vue 框架之鍵盤事件、健值修飾符、雙向數(shù)據(jù)綁定,希望對(duì)大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Vue動(dòng)態(tài)獲取數(shù)據(jù)后控件不可編輯問題
這篇文章主要介紹了Vue動(dòng)態(tài)獲取數(shù)據(jù)后控件不可編輯問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04詳解關(guān)于Vue版本不匹配問題(Vue packages version mismatch)
這篇文章主要介紹了詳解關(guān)于Vue版本不匹配問題(Vue packages version mismatch),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09巧妙運(yùn)用v-model實(shí)現(xiàn)父子組件傳值的方法示例
這篇文章主要介紹了巧妙運(yùn)用v-model實(shí)現(xiàn)父子組件傳值的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04vue項(xiàng)目多租戶環(huán)境變量的設(shè)置
本文主要介紹了vue項(xiàng)目多租戶環(huán)境變量的設(shè)置,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04element UI 中的 el-tree 實(shí)現(xiàn) checkbox&n
在日常項(xiàng)目開發(fā)中,會(huì)經(jīng)常遇到,樹形結(jié)構(gòu)的查詢方式,為了快速方便開發(fā),常常會(huì)使用到快捷的ui組件去快速搭樹形結(jié)構(gòu),這里我用的是 element ui 中的 el-tree,對(duì)element UI 中的 el-tree 實(shí)現(xiàn) checkbox 單選框及 bus 傳遞參數(shù)的方法感興趣的朋友跟隨小編一起看看吧2022-09-09Vue.js每天必學(xué)之?dāng)?shù)據(jù)雙向綁定
Vue.js每天必學(xué)之?dāng)?shù)據(jù)雙向綁定,如何進(jìn)行綁定,如何進(jìn)行數(shù)據(jù)雙向綁定,感興趣的小伙伴們可以參考一下2016-09-09