如何使用rangy从iframe中获取所选文本? [英] How to grab selected text from iframe using rangy?

查看:132
本文介绍了如何使用rangy从iframe中获取所选文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是后续问题 。我一直试图使用rangy从iframe中获取所选文本。该代码似乎适用于非iframe内容。根据 rangy的文档,它应该有效。

This is a followup question. I've been trying to grab the selected text from iframe using rangy. The code seems to be working for non-iframe content. As per the rangy's doc, it should work.

以下是演示: http:// jsfiddle .net / codef0rmer / UuJ5G /

推荐答案

您的示例中的问题是iframe的文档来自不同的来自主文档的域,这意味着在主文档中运行的脚本根本无法访问iframe中的文档。如果文档来自同一个域,浏览器只允许在文档之间编写脚本。

The problem in your example is that the iframe's document comes from a different domain from the main document, which means that script running in the main document simply cannot access the document within the iframe. The browser only allows scripting between documents if they come from the same domain.

如果文档确实来自同一个域,那么您可以使用<$ c $进行选择c> rangy.getIframeSelection():

If the documents do come from the same domain then you can get the selection using rangy.getIframeSelection():

http://jsfiddle.net/UuJ5G/42/

Rangy 1.3放宽对传递给 rangy.getSelection()和 rangy.createRange():两者都接受任何Window,Document或< ; iframe> 元素,因此您只需将iframe传递到 rangy.getSelection()

Rangy 1.3 relaxes the restrictions on parameters passed to rangy.getSelection() and rangy.createRange(): both will accept any of a Window, Document or <iframe> element, so you can just pass the iframe into rangy.getSelection():

http://jsfiddle.net/2d6Bk/56/

这篇关于如何使用rangy从iframe中获取所选文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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