国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

vue綁定class與行間樣式style詳解

2024-05-06 15:12:49
字體:
來源:轉載
供稿:網友

一、綁定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'        }      }    });  }            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 北川| 义乌市| 册亨县| 车险| 高密市| 绥德县| 林周县| 城口县| 确山县| 中方县| 皋兰县| 上饶市| 嘉鱼县| 承德县| 瑞安市| 炎陵县| 邵阳县| 剑河县| 永年县| 罗田县| 凤翔县| 黄冈市| 伊金霍洛旗| 淅川县| 靖远县| 广州市| 固阳县| 黄浦区| 漳浦县| 耒阳市| 长春市| 德昌县| 阳泉市| 安平县| 揭东县| 龙海市| 会昌县| 延寿县| 宜良县| 新野县| 东明县|