我想获取我点击的文本,因此,如果我点击单词“mother”,日志只会显示这个单词“mother”,即使它位于包含另一个单词的跨度内,
我使用了这段代码,但它甚至没有选择跨度:
function getSelectedText(e) { if(window.getSelection) return console.log(window.getSelection().toString()); else if(document.getSelection) return console.log(document.getSelection()); else if(document.selection) return console.log(document.selection.createRange().text); return console.log(""); } document.body.onmouseup = getSelectedText;
<div class="destination"> <span class="word">sister mother</span> <span class="word" >brother</span> <span class="word" >father</span> </div> <h1>hi</h1>