在Firefox扩展开发中,我想从chrome页面获取选定的内容,当我使用content.getSelection()时,它工作正常。
但是如果所选文本位于 iframe 中,则它不起作用。有人可以帮助我吗?
最佳答案
您需要调用getSelection()
关于window
已选择文本的对象。例如,如果有 <iframe id="iframe">
在内容文档中,您可以使用以下命令来获取该框架中的选择:
alert(content.document.getElementById("iframe").contentWindow.getSelection());
如果您不知道用户在哪里选择了文本,并且想要查看所有框架,那么您应该从顶层开始递归地遍历它们,如下所示:
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));
关于javascript - 当所选文本位于 iframe 中时,content.getSelection() 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10990690/