vue-dom元素绑定动态样式
众所周知,vue是操作dom元素的。那么如果有元素要动态绑定样式,这种需求,还是要通过改变数据来改变视图的样式。
例子:
在这个table自定义组件中,caption元素的宽度保持了table宽度一致,可以借助计算属性。
上代码:
<template>
<div>
<caption :style="{ width: capWidth }">
{{
tableTit
}}
</caption>
<table
:border="border"
:cellpadding="cellpadding"
:cellspacing="cellspacing"
:width="width"
id="customTable"
>
<tr v-for="item in dataTable" :key="item.val1">
<th>{{ item.val1 }}</th>
<th>{{ item.val2 }}</th>
</tr>
</table>
</div>
</template>
<script>
export default {
data() {
return {
// capWidth:""
};
},
props: {
border: {
type: String,
default: "2"
},
cellpadding: {
type: String,
default: "15"
},
cellspacing: {
type: String,
default: "20"
},
width: {
type: String,
default: "800"
},
tableTit: {
type: String,
default: "表格标题"
},
}
},
computed: {
// 动态的设置dom元素的宽度
capWidth() {
let ctx = this;
let capWidth = "";
if (ctx.width) {
capWidth = ctx.width;
}
return capWidth + "px";
}
},
watch: {}
};
</script>
<style>
caption {
width: 500px;
}
</style>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· Open-Sora 2.0 重磅开源!
2021-11-22 11-22 今天侃侃js的报错类型和符号