点击选择框,光标消失?
<template>
<div>
<!-- <textarea v-model="text" @click="textr"></textarea> -->
<div contenteditable="true" @click="textr" ref="editor" id="editor">
{{ content }}
</div>
<div class="flex" style="justify-content: center">
<el-button @click="toggleBold">加粗</el-button>
<el-button @click="toggleUnderline">下划线</el-button>
<el-button @click="toggleItalic">斜体</el-button>
<el-input
type="color"
v-model="color"
@input="changeColor"
style="width: 50px"
/>
<el-select v-model="fontSize" @change="changeFontSize" id="fontSize" @click="handleClick">
<el-option @click="handleClick"
v-for="data in fontSizeData"
:key="data.label"
:value="data.value"
>
{{ data.label }}
</el-option>
</el-select>
<el-select v-model="font" @change="changeFont" id="font">
<el-option value="Arial">Arial</el-option>
<el-option value="Verdana">Verdana</el-option>
<el-option value="Times New Roman">Times New Roman</el-option>
</el-select>
<el-button @click="insertImage">插入图片</el-button>
</div>
<div>{{ formattedText }}</div>
</div>
</template>
<script>
export default {
data() {
return {
text: '',
htmlText: '',
isBold: false,
isUnderline: false,
isItalic: false,
color: '#000000',
fontSizeData: [],
fontSize: '17px',
font: 'Arial',
selectionStart: 0,
selectionEnd: 0
};
},
created() {
this.fontSizeData = []
for (let i = 17; i <= 40; i++) {
this.fontSizeData.push({ label: i, value: i })
}
},
mounted() {
},
methods: {
handleClick(event) {
// 阻止事件冒泡传递到编辑器中
event.stopPropagation();
// 其他处理逻辑
// ...
},
toggleBold() {
document.execCommand("bold");
},
toggleUnderline() {
document.execCommand("underline");
},
toggleItalic() {
document.execCommand("italic");
},
changeColor() {
// Handle color change logic
document.execCommand("foreColor", false, this.color);
},
changeFontSize() {
// Handle font size change logic
document.execCommand("fontSize", false, this.fontSize)
},
changeFont() {
// Handle font change logic
document.execCommand("fontName", false, this.font);
},
insertImage() {
// Handle image insertion logic
},
textr(e) {
this.selectionStart = e.target.selectionStart
this.selectionEnd = e.target.selectionEnd
this.select = window.getSelection().toString()
console.lo
}
},
computed: {
formattedText() {
return ''
}
}
};
</script>
<style lang="scss" scoped>
div[contenteditable="true"] {
border: 1px solid #cfcbcb;
width: 80%;
margin: auto;
margin-top: 30px;
margin-bottom: 10px;
border-radius: 10px;
box-shadow: 0px 0px 6px #efefef;
outline: none;
text-align: start;
padding: 10px;
}
div[contenteditable="true"]:focus {
border-color: #666666;
}
</style>
这份代码,现在有一个问题就是点击选择框,编辑器的光标就消失了,使用@click.prevent并不会成功需要保留光标以及选中文本
回复
1个回答
test
2024-06-28
我解决了,绑定v-on:blur="focus"
然后focus里面this.$refs.editor.focus();
回复
适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容