Javascript函数返回函数代码或"{[native code]}",“我究竟做错了什么? [英] Javascript functions return lines of function code or "{[native code]}," what am I doing wrong?

查看:261
本文介绍了Javascript函数返回函数代码或"{[native code]}",“我究竟做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些代码以在可内容编辑的div中查找用户选择,我从

I am writing some code to find the user selection in a contenteditable div, I'm taking my code from 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().我做错了什么吗?

I'm testing it in Chrome and Firefox, if I do an alert(userSelection) within the function or an alert(findSelection();) outside the function, it returns function getSelection() {[native code]}. If I do console.log(findSelection();) it gives me getSelection(). Is there something I've done wrong?

推荐答案

将其更改为

if(window.getSelection){userSelection = window.getSelection();}

  if (window.getSelection) {userSelection = window.getSelection();}

(getSelection () )

(getSelection())

这篇关于Javascript函数返回函数代码或"{[native code]}",“我究竟做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆