【发布时间】:2017-02-17 12:47:50
【问题描述】:
下面是一个javaScript 函数,用于从iframe 中获取选定的内容
function getIframeSelectionText(iframe) {
var win = iframe.contentWindow;
var doc = win.document;
if (win.getSelection) {
return win.getSelection();
} else if (doc.selection && doc.selection.createRange) {
return doc.selection.createRange();
}
}
示例iframe 如下所示:
<iframe id="iframeId" type="html" src="__FILE_PATH__" width="100%" height="750px;"></iframe>
实现如下:
var content = getIframeSelectionText(document.getElementById("iframeId"));
我得到的是选择的文本。我需要HTML(因为我也想抓图)
我尝试将.html() 添加到getSelection(),但没有成功。
解决方案也可以涉及jQuery。
如何前进?
---编辑---
在这里可以找到一个关闭提示:window.getSelection() gives me the selected text, but I want the HTML
这里有对Selection 对象的引用:https://developer.mozilla.org/en-US/docs/Web/API/Selection
---可能的解决方案---
这是我面临的类似(不一样)问题的解决方案:https://stackoverflow.com/a/124929/2284357 另一个是Get Selected HTML in browser via Javascript
【问题讨论】:
标签: javascript jquery html iframe selection