Javascript 函数返回函数代码行或 "{[native code]},"我做错了什么?

标签 javascript contenteditable getselection

我正在编写一些代码来在 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/

相关文章:

javascript - window.getSelection() 如何获取 contenteditable javascript 中的当前节点

javascript - Snap.js移动面板插件添加 `destroy`方法

javascript - 使用 onClick 复制电子邮件表单

javascript - 如何删除 ContentEditable 中的 H1 格式(所见即所得)

javascript - 使用 jQuery 选择特定的 contenteditable div

javascript - 获取包含 HTML 内容的 contentEditable 区域中的插入符号(光标)位置

javascript - 如何使用 JavaScript 获取选中的文本

javascript - 更高效的数组搜索

javascript - Vue.js 中的 Messenger 扩展并将其数据用作全局变量

javascript - 如何防止在IE10中意外删除contenteditable ul?