【发布时间】:2010-05-12 16:11:01
【问题描述】:
我正在编写一些代码来在内容可编辑的 div 中查找用户选择,我从 this quirksmode article 获取我的代码。
function findSelection(){
var userSelection;
if (window.getSelection) {userSelection = window.getSelection;}
else if (document.selection){userSelection = document.selection.createRange();} // For microsoft
if (userSelection.text){return userSelection.text} //for Microsoft
else {return userSelection}
}
我正在 Chrome 和 Firefox 中对其进行测试,如果我在函数内执行 alert(userSelection) 或在函数外执行 alert(findSelection();),它会返回 function getSelection() {[native code]}。如果我这样做console.log(findSelection();),它会给我getSelection()。是不是我做错了什么?
【问题讨论】:
标签: javascript contenteditable getselection