我正在使用 Vue.jsNuxt在 SSR 中,我希望当我突出显示某些文本时获取该文本并对其执行操作。
我找到了一种方法,可以用一些文本来做到这一点<input>
但我希望在这里能够从段落中而不是从输入中@select一些文本。
这是我使用的方法<input>
:
<template>
<div>
<input @select="testFunction2()" value="TEXTE TO SELECT">
</div>
</template>
mounted() {
window.addEventListener('select',this.testFunction2,innerHTML)
},
methods:{
testFunction2(event) {
console.log(event.target.value.substring(event.target.selectionStart, event.target.selectionEnd));
},
我尝试用“innerHTML”替换“侦听器”代码中的“值”,但它似乎不起作用,
谢谢你!
编辑 :
我使用了特里提出的方法,它在段落中效果很好。
但是,当我想突出显示从 Vue 组件上的 v 循环中的数据库返回的对象上的某些文本时,我收到此错误:“未捕获的类型错误:无法在“节点”上执行“包含”:参数 1 不是”类型为“节点”。
在 HTMLDocument 中。
这是我在 vue 组件上返回的对象,该对象实际上是一个 html 文本,我使用 v-html 将其转换为纯文本:
<li v-for=" itemJournal in journalContent">
<b-row>
<b-col col lg="10" id="myNoteList" class="htmlNonUser grey-text text-
darken-2 myNoteList" style=""><p v-html="itemJournal.content"
ref="target"> </p>
</b-col>
</b-row>
mounted:function(){
document.addEventListener('mouseup', event => {
if (event.target === this.$refs.target ||
event.target.contains(this.$refs.target))
this.testFunction1();
});
},
testFunction1(event){
console.log(window.getSelection().toString());
},