[英]Vuejs - Focus element check
I have this code:我有这段代码:
<template>
<div id="app">
<span
contenteditable="true"
spellcheck="false"
style="width: 800px; display: block"
:v-text="textEl"
@focus="focused = true"
@blur="focused = false"
/>
{{ focused }}
</div>
</template>
<script>
export default {
data() {
return {
focused: false,
textEl: null,
};
},
methods: {
start() {
if (this.focused) {
this.textEl = "text with focus";
} else {
this.textEl = "text with not focus";
}
},
mounted() {
this.start();
},
},
components: {},
};
</script>
When the span is focused, I set focused
to true
, but, why does the method start
not run?当 span 聚焦时,我将focused
设置为true
,但是,为什么该方法start
不运行?
I need to show "text with focus"
when the element is focused, and "text with not focus"
when the element is not focused.当元素获得焦点时,我需要显示"text with focus"
,而当元素未获得焦点时,我需要显示“ "text with not focus"
。 How I can do it?我该怎么做? Method start
doesn't work inside mounted
.方法start
在mounted
中不起作用。
The method is only being called when the component is mounted.该方法仅在安装组件时被调用。
You either need to make start
a watcher so it runs whenever focused
changes:您要么需要start
一个观察者,以便它在focused
发生变化时运行:
watch: {
focused(newVal) {
if (newVal) {
this.textEl = "text with focus";
} else {
this.textEl = "text with not focus";
}
}
}
Or call start
in the event-handlers:或者在事件处理程序中调用start
:
@focus="start()"
@blur="start()"
...
start() {
this.focused = !this.focused;
if (this.focused) {
this.textEl = "text with focus";
} else {
this.textEl = "text with not focus";
}
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.