我正在编写一些代码来在 contenteditable 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 代码]}
。如果我这样做 console.log(findSelection();)
它会给我 getSelection()
。我是不是做错了什么?
最佳答案
getSelection 是一个函数...您需要执行它来获取选择吗?
if (window.getSelection) {userSelection = window.getSelection();}
关于Javascript 函数返回函数代码行或 "{[native code]},"我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2820650/