简体   繁体   English

双击一个单词javascript React.js时触发事件

[英]Trigger event on double click on a word, javascript React.js

I have a component in Reactjs. 我在Reactjs中有一个组件。 This has a p tag. 这有一个p标签。 Inside that there is text. 里面有文字。

I want that when ever user double click on any word, it should trigger a function, in which I get the selected text, so that I can save that word in an array. 我希望当用户双击任何单词时,它都应该触发一个函数,在该函数中我可以获取选定的文本,以便可以将该单词保存在数组中。

Please help. 请帮忙。

function getSelectionText() {
    var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
        text = document.selection.createRange().text;
    }
    return text;
}

This should work for all browsers. 这应该适用于所有浏览器。 Attach it to mouseup of your p tag. 将其附加到p标签的mouseup上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM