我有一个包含多行的表。每列都有一些文本。我想在选择/突出显示字符串文本的任何部分时创建一个弹出窗口。我研究了整个网络,但找不到解决方案。有人能帮忙吗?
$('table').on('mouseup', function(){
var selectedText = window.getSelection().toString();
if (selectedText) {
alert('Some text was selected');
}
})
http://codepen.io/anon/pen/PZoxZB
此处回答了相同的问题
function getSelectedText() {
var text = "";
if (typeof window.getSelection != "undefined") {
text = window.getSelection().toString();
} else if (typeof document.selection != "undefined" && document.selection.type == "Text") {
text = document.selection.createRange().text;
}
return text;
}
function doSomethingWithSelectedText() {
var selectedText = getSelectedText();
if (selectedText) {
alert("Got selected text " + selectedText);
}
}
document.onmouseup = doSomethingWithSelectedText;
document.onkeyup = doSomethingWithSelectedText;