【发布时间】:2013-12-23 13:05:34
【问题描述】:
我正在尝试在 HTML 页面上获取选择文本。
我使用下面的代码,而 window.getSelection() 的 textarea 接缝在 Firefox 中不起作用,
但在 Google Chrome 中运行良好。
- 我使用的是 firefox 24 和 chrome 27。
这是一个示例: http://jsfiddle.net/AVLCY/
HTML:
<div>Text in div</div>
<textarea>Hello textarea</textarea>
<div id='debug'></div>
JS:
$(document).on('mouseup','body',function(){
$("#debug").html("You select '" + getSelectionText() + "'");
});
function getSelectionText() {
if (window.getSelection) {
try {
// return "" in firefox
return window.getSelection().toString();
} catch (e) {
console.log('Cant get selection text')
}
}
// For IE
if (document.selection && document.selection.type != "Control") {
return document.selection.createRange().text;
}
}
【问题讨论】:
-
这是由于Firefox bug filed in 2001(是的,14 年前)。
标签: javascript firefox