当选择的文本在iframe中时,content.getSelection()不工作? [英] content.getSelection() is not working when selected text is in iframe?
问题描述
getSelection()
在已选择文本的窗口
对象上。例如,如果在内容文档中有一个< iframe id =iframe>
,则可以使用以下命令来获取该帧中的选择:(iframe)。contentWindow.getSelection());这是一个非常简单的方法。 如果您不知道用户选择哪个文本,并且想要查看所有框架,那么您应该从顶层递归地开始,像这样:
$ 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屋!