javascript - Vuejs - 焦点元素检查
问题描述
我有这个代码:
<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>
完整代码。
当跨度集中时,我设置focused
为true
,但是,为什么该方法start
不运行?
我需要显示"text with focus"
元素何时聚焦,"text with not focus"
何时元素未聚焦。我该怎么做?方法start
在里面不起作用mounted
。
解决方案
仅在安装组件时调用该方法。
您要么需要创建start
一个观察者,以便它在focused
更改时运行:
watch: {
focused(newVal) {
if (newVal) {
this.textEl = "text with focus";
} else {
this.textEl = "text with not focus";
}
}
}
或调用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";
}
},
推荐阅读
- binary - 编写一个程序来添加一个二进制文件
- c# - 优化涉及 Where Newbie C# / LINQ 问题的 LINQ 查询
- kubernetes - srimzi 0.14:Kafka 代理失败的身份验证
- c++ - const_cast
(char* const) 不是左值? - angular6 - 如何将多个 pdf 文件下载为 zip 文件?
- arrays - 基于目标条件从数组中随机采样元素
- python - Django rest self-reference ForeignKey error 禁止直接赋值给相关集合的反面。改用 parent.set()
- javascript - Eureka Server 在 Spring Boot 中启动时出现错误
- neo4j - Cypher 推荐分数
- swiftui - Xcode 版本 11.0 (11A420a) (.presentation) 已弃用?