admin管理员组文章数量:1399903
I am trying to give one of two classes to an element depending on one of the three possible input variables. My vue.js code
<input type='text' class='inputwordtext' v-bind:class="[{(wordupload.firstchoice.selected == 'Zinnenlijst') : wordlongwidth}, {(wordupload.firstchoice.selected != 'Zinnenlijst') : wordshortwidth}]">
If wordupload.firstchoice.selected == Zinnenlijst it should get the class wordlongwidth, otherwise it should get the class wordshortwidth, how can this be done?
I am trying to give one of two classes to an element depending on one of the three possible input variables. My vue.js code
<input type='text' class='inputwordtext' v-bind:class="[{(wordupload.firstchoice.selected == 'Zinnenlijst') : wordlongwidth}, {(wordupload.firstchoice.selected != 'Zinnenlijst') : wordshortwidth}]">
If wordupload.firstchoice.selected == Zinnenlijst it should get the class wordlongwidth, otherwise it should get the class wordshortwidth, how can this be done?
Share Improve this question asked Oct 9, 2017 at 17:17 JFugger_jrJFugger_jr 3131 gold badge4 silver badges15 bronze badges2 Answers
Reset to default 4You could do this with a single inline expression using a tenary operator:
<input
type='text'
class='inputwordtext'
:class="wordupload.firstchoice.selected === 'Zinnenlijst' ? 'wordlongwidth' : 'wordshortwidth'"
>
But, it would be more readable to make it a puted property:
puted: {
inputClass() {
let selected = this.wordupload.firstchoice.selected;
return (selected === 'Zinnenlijst') ? 'wordlongwidth' : 'wordshortwidth';
}
}
And then reference that puted property in your template:
<input type='text' class='inputwordtext' :class="inputClass">
The vue.js documentation suggests to use a ternary expression and to bine v-bind:class
and your regular class
like so:
<input type='text' :class="[wordupload.firstchoice.selected === 'Zinnenlijst' ? 'wordlongwidth' : 'wordshortwidth', 'inputwordtext']">
To see where they talk about this and learn more about class binds check out their documentation:
https://v2.vuejs/v2/guide/class-and-style.html#Array-Syntax
And you learn more about ternary expressions check out this source:
https://www.w3schools./js/js_parisons.asp
本文标签: javascriptVbind class multiple optionsStack Overflow
版权声明:本文标题:javascript - V-bind class multiple options - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744123056a2591828.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论