vue中使用contenteditable="true",怎么监听到文本编辑了,并且获取编辑后文本的内容

360 阅读1分钟

3196dde90a81200f0054a2ec59801c02.png

在 Vue 中,如果你有一个 contenteditable="true" 的元素,你可以通过监听 input 事件来捕获文本编辑的变化,并获取编辑后的文本内容。以下是一个简单的示例,展示了如何实现这一点:

<template> 
<div> 
    <div contenteditable="true" @input="handleInput"> 
        {{ text }}
    </div>
</div> 
</template>
<script> 
export default {
data() {
    return { 
        text: '开始编辑吧...' 
    }
},
methods: { 
    handleInput(event) { 
        // 获取编辑后的文本内容 
        console.log(event.target.innerHTML) 
    } 
}
}; 
</script>