首页 > 语言 > JavaScript > 正文

vue基础之v-bind属性、class和style用法分析

2024-05-06 15:41:44
字体:
来源:转载
供稿:网友

本文实例讲述了vue基础之v-bind属性、class和style用法。分享给大家供大家参考,具体如下:

一、属性

属性:

v-bind:src=""
width/height/title....

简写:

:src=""    推荐

<img src="{{url}}" alt="">    效果能出来,但是会报一个404错误
<img v-bind:src="url" alt="">    效果可以出来,不会发404请求

window.onload=function(){      new Vue({        el:'#box',        data:{          url:'https://www.baidu.com/img/bd_logo1.png',          w:'200px',          t:'这是一张美丽的图片'        },        methods:{        }      });    };
<div id="box">    <!--<img src="{{url}}" alt="">-->    <img :src="url" alt="" :width="w" :title="t">  </div>

二、class和style

:class=""     v-bind:class=""
:style=""     v-bind:style=""
:class="[red]"     red是数据
:class="[red,b,c,d]"

<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title></title>  <style>    .red{      color: red;    }    .blue{      background: blue;    }  </style>  <script src="vue.js"></script>  <script>    window.onload=function(){      new Vue({        el:'#box',        data:{          claOne:'red',//这里的red是样式class类名          claTwo:'blue'        },        methods:{        }      });    };  </script></head><body>  <div id="box">    <!--这里的calOne,calTwo指data里的数据-->    <strong :class="[claOne,claTwo]">文字...</strong>  </div></body></html>

:class="{red:true, blue:false}"//这里是{ json}

<style>    .red{      color: red;    }    .blue{      background: blue;    }  </style>  <script src="vue.js"></script>  <script>    window.onload=function(){      new Vue({        el:'#box',        data:{        },        methods:{        }      });    };  </script><div id="box">    <strong :class="{red:true,blue:true}">文字...</strong>  </div>
<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title></title>  <style>    .red{      color: red;    }    .blue{      background: blue;    }  </style>  <script src="vue.js"></script>  <script>    window.onload=function(){      new Vue({        el:'#box',        data:{          a:true,          b:false        },        methods:{        }      });    };  </script></head><body>  <div id="box">    <strong :class="{red:a,blue:b}">文字...</strong>  </div></body></html>            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选