使用 css 在 vuejs 中输入值:not[value=""]
input value in vuejs using css :not[value=""]
我在 VueJS 项目中使用 Bootstrap-Vue,使用 class..
是不可能的
我有下一个输入
<div class="input__wrapper">
<b-form-input
v-model="idOrNameSelected"
class="textfield"
:class="{'input--empty': idOrNameSelected != null}"
@keydown.enter.native="onSubmit" />
<label
class="input__label">Service id or name</label>
</div>
在我的脚本部分,我定义了 idOrNameSelected,例如:
data () {
return {
idOrNameSelected: ''
}
}
在我的 scss 文件中,我有一个像
这样的规则
&:focus ~ .input__label,
&:not([value=""]) ~ .input__label {
top: 8px;
pointer-events: none;
}
当我在输入中输入任何文本时,从未使用此 css 规则,为什么?????
谢谢
问题来了。 CSS 逻辑不知道 Vue 观察者或 v-model
所以它不会更新你的想法。退后一步,试试这个简单的例子:
HTML
<input class="in" type="text" value="bar" />
<label class="lab">Test</label>
CSS
.in:not([value="foo"]) ~ .lab {
color: crimson;
}
如您所见,标签现在是红色的。现在尝试更改 value="foo"
您将看到 label
切换颜色。但是,你在这里应该注意的是,它在 CSS 本身上没有任何类型的双向绑定,而实际上只是在实际中采用 当前值 DOM.
为了向您提供实际的解决方案,我将在本例中使用 class 绑定。
你可以在这里阅读它们:https://vuejs.org/v2/guide/class-and-style.html
本质上,它允许您根据某些真实条件动态地 add/remove 一个 class。你实际上可以在那里使用你的 v-model。
这是我将如何做的一个例子:
<template>
<div id="app">
<input type="text" v-model="model">
<label :class="{error: model === ''}">Label</label>
</div>
</template>
<script>
export default {
name: "App",
data() {
return { model: "" };
}
};
</script>
<style scoped>
.error {
color: crimson;
}
</style>
@dev-cyprium 关于 Vue 在使用 v-model 时没有将属性 value
放在输入元素上是正确的(value
实际上是元素上的 domProp)
尽管如此,您可以使用数据属性做一个技巧:
<template>
<div>
<b-form-input id="the-input" v-model="value" :data-value="value"></b-form-input>
<label for="the-input">Hello World</label>
</div>
</template>
<style>
input.form-control ~ label {
color: red;
}
input.form-control[data-value=""] ~ label {
color: blue;
}
</style>
<script>
export default {
data() {
return {
value: ''
}
}
}
</script>
我在 VueJS 项目中使用 Bootstrap-Vue,使用 class..
是不可能的我有下一个输入
<div class="input__wrapper">
<b-form-input
v-model="idOrNameSelected"
class="textfield"
:class="{'input--empty': idOrNameSelected != null}"
@keydown.enter.native="onSubmit" />
<label
class="input__label">Service id or name</label>
</div>
在我的脚本部分,我定义了 idOrNameSelected,例如:
data () {
return {
idOrNameSelected: ''
}
}
在我的 scss 文件中,我有一个像
这样的规则&:focus ~ .input__label,
&:not([value=""]) ~ .input__label {
top: 8px;
pointer-events: none;
}
当我在输入中输入任何文本时,从未使用此 css 规则,为什么?????
谢谢
问题来了。 CSS 逻辑不知道 Vue 观察者或 v-model
所以它不会更新你的想法。退后一步,试试这个简单的例子:
HTML
<input class="in" type="text" value="bar" />
<label class="lab">Test</label>
CSS
.in:not([value="foo"]) ~ .lab {
color: crimson;
}
如您所见,标签现在是红色的。现在尝试更改 value="foo"
您将看到 label
切换颜色。但是,你在这里应该注意的是,它在 CSS 本身上没有任何类型的双向绑定,而实际上只是在实际中采用 当前值 DOM.
为了向您提供实际的解决方案,我将在本例中使用 class 绑定。 你可以在这里阅读它们:https://vuejs.org/v2/guide/class-and-style.html
本质上,它允许您根据某些真实条件动态地 add/remove 一个 class。你实际上可以在那里使用你的 v-model。
这是我将如何做的一个例子:
<template>
<div id="app">
<input type="text" v-model="model">
<label :class="{error: model === ''}">Label</label>
</div>
</template>
<script>
export default {
name: "App",
data() {
return { model: "" };
}
};
</script>
<style scoped>
.error {
color: crimson;
}
</style>
@dev-cyprium 关于 Vue 在使用 v-model 时没有将属性 value
放在输入元素上是正确的(value
实际上是元素上的 domProp)
尽管如此,您可以使用数据属性做一个技巧:
<template>
<div>
<b-form-input id="the-input" v-model="value" :data-value="value"></b-form-input>
<label for="the-input">Hello World</label>
</div>
</template>
<style>
input.form-control ~ label {
color: red;
}
input.form-control[data-value=""] ~ label {
color: blue;
}
</style>
<script>
export default {
data() {
return {
value: ''
}
}
}
</script>