我想在iframe设计模式中获取所选内容。我正在使用以下代码。
function getIframeSelectionText(iframe) {
var win = iframe.contentWindow;
var doc = iframe.contentDocument || win.document;
if (win.getSelection) {
return win.getSelection();
} else if (doc.selection && doc.selection.createRange) {
return doc.selection.createRange().text;
}
}
我能够获取文本,因为我无法获得也被选中的图像。请帮忙。
答案 0 :(得分:4)
您可以使用Range Object来实现这一目标:
function getImg(iframe) {
var win = iframe.contentWindow;
var doc = iframe.contentDocument || win.document;
// get Range object
var range = win.getSelection().getRangeAt(0)
// now you get a copy of the nodes that been selected
var fragment = range.cloneContents()
// now you can do whatever you want with fragment,
// such as find img element
var imgs = fragment.querySelectorAll('img')
}