Vue分別運(yùn)用class綁定和style綁定通過(guò)點(diǎn)擊實(shí)現(xiàn)樣式切換
Vue官方文檔
https://cn.vuejs.org/v2/guide...
<div v-bind:class="[activeClass, errorClass]"></div> 可以簡(jiǎn)寫成 <div :class="[activeClass, errorClass]"></div>
class綁定
<!-- * @Author: [you name] * @Date: 2021-10-08 15:15:52 * @LastEditors: [you name] * @LastEditTime: 2021-10-08 22:46:18 * @Description: --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.13/vue.js"></script> <style> /* 點(diǎn)擊前的樣式 */ .class1 { background-color: #fff; color: #333; } /* 點(diǎn)擊之后的樣式 */ .class2 { background-color: #f52819; color: #fff; } /* 給按鈕設(shè)置樣式 */ button { width: 80px; height: 40px; border-radius: 5px; border: 2px solid rgb(179, 167, 167); background-color: #fff; } </style> </head> <body> <div id="app"> <!-- 分別給按鈕設(shè)置點(diǎn)擊事件 --> <button @click='handler1' :class="[isYes1? 'class1' : 'class2']">按鈕1</button> <button @click='handler2' :class="[isYes2? 'class1' : 'class2']">按鈕2</button> <button @click='handler3' :class="[isYes3? 'class1' : 'class2']">按鈕3</button> </div> <script> // 第二種方法 let vm = new Vue({ el:'#app', data:{ isYes1:true, isYes2:true, isYes3:true, }, methods:{ handler1(){ this.isYes1 = false, this.isYes2 = true, this.isYes3 = true, console.log('第一個(gè)點(diǎn)擊事件'); }, handler2(){ this.isYes2 = false, this.isYes1 = true, this.isYes3 = true, console.log('第二個(gè)點(diǎn)擊事件'); }, handler3(){ this.isYes3 = false, this.isYes2 = true, this.isYes1 = true, console.log('第三個(gè)點(diǎn)擊事件'); }, } }) </script> </body> </html>
style綁定
<!-- * @Author: [you name] * @Date: 2021-10-08 15:15:52 * @LastEditors: [you name] * @LastEditTime: 2021-10-08 22:54:40 * @Description: --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.13/vue.js"></script> <style> /* 給按鈕設(shè)置樣式 */ button { width: 80px; height: 40px; border-radius: 5px; border: 2px solid rgb(179, 167, 167); background-color: #fff; } </style> </head> <body> <div id="app"> <!-- style綁定,這里是表達(dá)式結(jié)果類型為字符串,為展示點(diǎn)擊按鈕改變樣式,使用的是三目運(yùn)算, 在第一步中設(shè)置了一個(gè)可用于判斷的數(shù)據(jù),如果該數(shù)據(jù)值和按鈕內(nèi)容一樣的話,則會(huì)觸發(fā)點(diǎn)擊事件, 該style樣式設(shè)置為要改變的樣式,即data中設(shè)置的styCss樣式 --> <button :style='isActive =="按鈕1" ? styCss : ""' @click='changeHandler'>按鈕1</button> <button :style='isActive =="按鈕2" ? styCss : ""' @click='changeHandler'>按鈕2</button> <button :style='isActive =="按鈕3" ? styCss : ""' @click='changeHandler'>按鈕3</button> </div> <script> let vm = new Vue({ el: '#app', data: { // 設(shè)置一個(gè)數(shù)據(jù)來(lái)進(jìn)行判斷,其初始值設(shè)為空字符串,就會(huì)顯示原始樣式 isActive: '', // 在數(shù)據(jù)模型中設(shè)置經(jīng)點(diǎn)擊后要變換的樣式,這里聲明一個(gè)對(duì)象,用在按鈕的綁定上,點(diǎn)擊后切換的樣式 styCss: { background: 'red', color: 'white' } }, methods: { // 為點(diǎn)擊事件實(shí)現(xiàn)三按鈕之間的互斥效果,即點(diǎn)擊一個(gè)按鈕,該按鈕的樣式改變, //其他的不變,點(diǎn)擊另一個(gè)時(shí),前一個(gè)按鈕的樣式還原,當(dāng)前按鈕樣式改變, //那么就需要在點(diǎn)擊方法中添加將目標(biāo)源元素的文本值賦予需要進(jìn)行判斷的數(shù)據(jù)時(shí), //當(dāng)點(diǎn)擊的按鈕的內(nèi)容和判斷的條件一樣時(shí),成功觸發(fā)該點(diǎn)擊事件,實(shí)現(xiàn)切換并且改變樣式的效果。 changeHandler(event) { this.isActive = event.target.innerText } } }) </script> </body> </html>
以上就是Vue--分別運(yùn)用class綁定和style綁定,通過(guò)點(diǎn)擊實(shí)現(xiàn)樣式的切換的詳細(xì)內(nèi)容,更多關(guān)于Vue-運(yùn)用class style綁定點(diǎn)擊樣式切換的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Ant Design編寫登錄和注冊(cè)頁(yè)面的教程
這篇文章主要介紹了Ant Design編寫登錄和注冊(cè)頁(yè)面的教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04Ant Design Vue 添加區(qū)分中英文的長(zhǎng)度校驗(yàn)功能
這篇文章主要介紹了Ant Design Vue 添加區(qū)分中英文的長(zhǎng)度校驗(yàn)功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下功能,2020-01-01前端框架Vue父子組件數(shù)據(jù)雙向綁定的實(shí)現(xiàn)
Vue項(xiàng)目中經(jīng)常使用到組件之間的數(shù)值傳遞,實(shí)現(xiàn)的方法很多,但是原理上基本大同小異。這篇文章將給大家介紹Vue 父子組件數(shù)據(jù)單向綁定與Vue 父子組件數(shù)據(jù)雙向綁定的對(duì)比從而認(rèn)識(shí)雙向綁定2021-09-09淺談Vue為什么不能檢測(cè)數(shù)組變動(dòng)
這篇文章主要介紹了淺談Vue為什么不能檢測(cè)數(shù)組變動(dòng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10vue項(xiàng)目中常用解決跨域的方法總結(jié)(CORS和Proxy)
在vue項(xiàng)目中,一般我們會(huì)遇到跨域的問(wèn)題,vue項(xiàng)目中解決跨域是非常簡(jiǎn)單的,下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目中常用解決跨域的方法,主要解釋CROS和Proxy兩種方式,需要的朋友可以參考下2022-12-12