当选择的文本在iframe中时,content.getSelection()不工作? [英] content.getSelection() is not working when selected text is in iframe?

查看:609
本文介绍了当选择的文本在iframe中时,content.getSelection()不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Firefox扩展开发中,当我使用content.getSelection()时,我想从chrome页面中获取选定的内容,其工作正常。但是,如果选中的文本是在iframe中,它不能正常工作。任何人都可以帮助我?

解决方案您需要调用 getSelection()在已选择文本的窗口对象上。例如,如果在内容文档中有一个< iframe id =iframe> ,则可以使用以下命令来获取该帧中的选择:

如果您不知道用户选择哪个文本,并且想要查看所有框架,那么您应该从顶层递归地开始,像这样:
$ b

function getSelection(wnd )
{
var result = wnd.getSelection();

for(var i = 0;!result&& i< wnd.frames.length; i ++)
result = getSelection(wnd.frames [i]);

返回结果;
}

alert(getSelection(content));


In Firefox Extension development, I wants to get selected content from the chrome page, when I use content.getSelection(), its working fine .

But if the selected text is in the iframe, its not working. anyone can help me ?

解决方案

You need to call getSelection() on the window object where text has been selected. For example, if there is an <iframe id="iframe"> in the content document, you would use the following to get the selection in that frame:

alert(content.document.getElementById("iframe").contentWindow.getSelection());

If you don't know where the user selected text and you want to look at all frames then you should go through them recursively starting at the top level, something like this:

function getSelection(wnd)
{
  var result = wnd.getSelection();

  for (var i = 0; !result && i < wnd.frames.length; i++)
    result = getSelection(wnd.frames[i]);

  return result;
}

alert(getSelection(content));

这篇关于当选择的文本在iframe中时,content.getSelection()不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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