一、綁定class屬性的方式
1、通過數組的方式,為元素綁定多個class
<style> .red { color:red; /*color:#ff8800;*/ } .bg { background: #000; /*background: green;*/ } </style> window.onload = function(){ var c = new Vue({ el : '#box', data : { red_color : 'red', bg_color : 'bg' } }); } <div id="box"> <span :class="[red_color,bg_color]">this is a test string</span> </div>上例為span 綁定2個class,通過設定red_color和bg_color的值,找到對應的class類名
2、通過控制class的true或者false,來使用對應的class類名, true: 使用該class, false: 不使用該class
<style> .red { color:red; } .bg { background: #000; } </style> window.onload = function(){ var c = new Vue({ el : '#box', data : { } }); } <div id="box"> <span :class="{red:true,bg:true}">this is a test string</span> </div>3、這種方式,跟第二種差不多,只不過是把class的狀態true/false用變量來代替
<style> .red { color:red; } .bg { background: #000; } </style> window.onload = function(){ var c = new Vue({ el : '#box', data : { r : true, b : false } }); } <div id="box"> <span :class="{red:r,bg:b}">this is a test string</span> </div>4、為class屬性綁定整個json對象
<style> .red { color:red; } .bg { background: #000; } </style> window.onload = function(){ var c = new Vue({ el : '#box', data : { json : { red : true, bg : false } } }); } <div id="box"> <span :class="json">this is a test string</span> </div>二、綁定style行間樣式的多種方式
1、使用json格式,直接在行間寫
window.onload = function(){ var c = new Vue({ el : '#box', }); } <div id="box"> <span :style="{color:'red',background:'#000'}">this is a test string</span> </div>2、把data中的對象,作為數組的某一項,綁定到style
window.onload = function(){ var c = new Vue({ el : '#box', data : { c : { color : 'green' } } }); } <div id="box"> <span :style="[c]">this is a test string</span> </div>3、跟上面的方式差不多,只不過是為數組設置了多個對象
window.onload = function(){ var c = new Vue({ el : '#box', data : { c : { color : 'green' }, b : { background : '#ff8800' } } }); }<div id="box"> <span :style="[c,b]">this is a test string</span> </div>
4、直接把data中的某個對象,綁定到style
window.onload = function(){ var c = new Vue({ el : '#box', data : { a : { color:'yellow', background : '#000' } } }); }<div id="box"> <span :style="a">this is a test string</span></div>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答