我想取得我點擊的文本,因此,如果我點擊單字“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>