功能效果图
功能描述
- input文本框,通过点击下方关键词,在文本框中插入该词
- 光标移动到文本之间,再点击下方关键词,在光标的位置插入该词
- 插入关键词后,文本框获取焦点,并将光标的位置定位到关键词后
代码实现
- 给
<el-input>
标签加上失去焦点事件 @blur="handleInputBlur"
- 获取光标位置
- 点击关键词时候,将文本内容从光标位置进行拆分,拼接关键词,设置焦点,处理光标位置
<template>
<div class="input-creative-word">
<el-input
ref="inputWord"
v-model="proxyValue"
class="input-item"
type="textarea"
:autosize="{minRows: 6}"
@blur="handleInputBlur"
/>
<div class="main-word">
<div class="main-word-left">
<span class="main-word-label">插入动态词包: </span>
<el-button
v-for="n in commonWordList"
:key="n.creativeWordId"
class="button-word"
type="text"
@click="btnClick(n.name)">+{{ n.name }}</el-button>
</div>
</div>
</div>
</template>
<script>
export default {
name: "InputCreativeWord",
data() {
return {
proxyValue:"",
commonWordList: [
{ name: "地点", creativeWordId: 1 },
{ name: "日期", creativeWordId: 2 },
{ name: "星期", creativeWordId: 3 },
{ name: "年龄", creativeWordId: 4 }
],
cursorIndex: "", // 光标位置
};
},
methods: {
// 获取光标位置
handleInputBlur(e) {
this.cursorIndex = e.srcElement.selectionStart;
},
btnClick(label) {
// 将文本内容在光标位置进行拆分
const txt = this.proxyValue;
const start = txt.substring(0, this.cursorIndex);
const end = txt.substring(this.cursorIndex, txt.length);
// 插入关键词
this.proxyValue = start + `{${label}}` + end;
// 获取文本框,设置焦点,处理光标位置
if (this.$refs.inputWord) {
// this.$refs.inputWord.focus();
this.$nextTick(() => {
var a = this.$refs.inputWord.$el.firstElementChild;
a.focus();
a.selectionStart = this.cursorIndex + label.length + 2;
a.selectionEnd = this.cursorIndex + label.length + 2;
});
}
}
}
};
</script>
本网站是一个以CSS、JavaScript、Vue、HTML为核心的前端开发技术网站。我们致力于为广大前端开发者提供专业、全面、实用的前端开发知识和技术支持。
在本网站中,您可以学习到最新的前端开发技术,了解前端开发的最新趋势和最佳实践。我们提供丰富的教程和案例,让您可以快速掌握前端开发的核心技术和流程。
本网站还提供一系列实用的工具和插件,帮助您更加高效地进行前端开发工作。我们提供的工具和插件都经过精心设计和优化,可以帮助您节省时间和精力,提升开发效率。
除此之外,本网站还拥有一个活跃的社区,您可以在社区中与其他前端开发者交流技术、分享经验、解决问题。我们相信,社区的力量可以帮助您更好地成长和进步。
在本网站中,您可以找到您需要的一切前端开发资源,让您成为一名更加优秀的前端开发者。欢迎您加入我们的大家庭,一起探索前端开发的无限可能!
- 给
<el-input>
标签加上失去焦点事件@blur="handleInputBlur"
- 获取光标位置
- 点击关键词时候,将文本内容从光标位置进行拆分,拼接关键词,设置焦点,处理光标位置
<template>
<div class="input-creative-word">
<el-input
ref="inputWord"
v-model="proxyValue"
class="input-item"
type="textarea"
:autosize="{minRows: 6}"
@blur="handleInputBlur"
/>
<div class="main-word">
<div class="main-word-left">
<span class="main-word-label">插入动态词包: </span>
<el-button
v-for="n in commonWordList"
:key="n.creativeWordId"
class="button-word"
type="text"
@click="btnClick(n.name)">+{{ n.name }}</el-button>
</div>
</div>
</div>
</template>
<script>
export default {
name: "InputCreativeWord",
data() {
return {
proxyValue:"",
commonWordList: [
{ name: "地点", creativeWordId: 1 },
{ name: "日期", creativeWordId: 2 },
{ name: "星期", creativeWordId: 3 },
{ name: "年龄", creativeWordId: 4 }
],
cursorIndex: "", // 光标位置
};
},
methods: {
// 获取光标位置
handleInputBlur(e) {
this.cursorIndex = e.srcElement.selectionStart;
},
btnClick(label) {
// 将文本内容在光标位置进行拆分
const txt = this.proxyValue;
const start = txt.substring(0, this.cursorIndex);
const end = txt.substring(this.cursorIndex, txt.length);
// 插入关键词
this.proxyValue = start + `{${label}}` + end;
// 获取文本框,设置焦点,处理光标位置
if (this.$refs.inputWord) {
// this.$refs.inputWord.focus();
this.$nextTick(() => {
var a = this.$refs.inputWord.$el.firstElementChild;
a.focus();
a.selectionStart = this.cursorIndex + label.length + 2;
a.selectionEnd = this.cursorIndex + label.length + 2;
});
}
}
}
};
</script>
本网站是一个以CSS、JavaScript、Vue、HTML为核心的前端开发技术网站。我们致力于为广大前端开发者提供专业、全面、实用的前端开发知识和技术支持。
在本网站中,您可以学习到最新的前端开发技术,了解前端开发的最新趋势和最佳实践。我们提供丰富的教程和案例,让您可以快速掌握前端开发的核心技术和流程。
本网站还提供一系列实用的工具和插件,帮助您更加高效地进行前端开发工作。我们提供的工具和插件都经过精心设计和优化,可以帮助您节省时间和精力,提升开发效率。
除此之外,本网站还拥有一个活跃的社区,您可以在社区中与其他前端开发者交流技术、分享经验、解决问题。我们相信,社区的力量可以帮助您更好地成长和进步。
在本网站中,您可以找到您需要的一切前端开发资源,让您成为一名更加优秀的前端开发者。欢迎您加入我们的大家庭,一起探索前端开发的无限可能!