我正在尝试为 Windows 编写一个 chrome 扩展,它将任何选定的文本从页面复制到剪贴板。我正在使用 jquery 来执行 java 脚本部分。
我如何获取该页面中任何选定/突出显示的文本?换句话说,是否有一个事件监听器,当文本的任何部分突出显示时会触发该事件监听器。
最佳答案
function getSelected() {
if (window.getSelection) return window.getSelection();
if (document.getSelection) return document.getSelection();
if (document.selection) return document.selection.createRange().text;
}
document.onmouseup = function () {
getSelected(); // => "Something you've sele..."
};
<小时/>
Demo
关于jquery 从页面中获取选择文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12382943/