javascript 在 $refs 中添加样式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49572850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding style in $refs?
提问by BGTabulation BGTabulate
Can't add style in $refs attribute. Cannot set property 'cssText' of undefined. Is this possible? Can't find any similar to this
无法在 $refs 属性中添加样式。无法设置未定义的属性“cssText”。这可能吗?找不到类似的
this.$refs['ticketCode_'+this.resoTrans_id].style.cssText =
"background-color:#66BB6A !important; border:1px solid #66BB6A !important; color:#fff !important;";
Printing out without .style seems work fine console.log(this.$refs['ticketCode_'+this.resoTrans_id])
没有 .style 打印出来似乎工作正常 console.log(this.$refs['ticketCode_'+this.resoTrans_id])
VueComponent?{_uid: 493, _isVue: true, $options:
回答by Benjamin Schmidt
You're not supposed to use cssText but instead use the JavaScript API to set styles (which you can do on $refs, too):
你不应该使用 cssText 而是使用 JavaScript API 来设置样式(你也可以在 $refs 上这样做):
let $ref = this.$refs['ticketCode_' + this.resoTrans_id]
$ref.style.backgroundColor = '#66bb6a';
$ref.style.border = '1px solid #66bb6a';
...
Even better is to utilize Vue's power for that directly in your template:
更好的是直接在您的模板中利用 Vue 的强大功能:
<your-element ref="'ticketCode_' + resoTrans_id" :style="{ backgroundColor: '#66bb6a', border: '1px solid #66bb6a' /* ... */ }" />

