经过一番尝试,我得到了函数,它返回了我在iframe中选择的html,问题是它返回了我的本地路径的href属性,如何才能获得完整的url路径/绝对路径,然后再将其从iframe? 这是功能:
function getSelectionHtml(iframe) {
var html = "";
var win = iframe.contentWindow;
var doc = win.document;
if (typeof win.getSelection != "undefined") {
var sel = win.getSelection();
if (sel.rangeCount) {
var container = doc.createElement("div");
for (var i = 0, len = sel.rangeCount; i < len; ++i) {
container.appendChild(sel.getRangeAt(i).cloneContents());
}
html = container.innerHTML;
}
} else if (typeof doc.selection != "undefined") {
if (doc.selection.type == "Text") {
html = doc.selection.createRange().htmlText;
}
}
return html;
}
已解决:实际上我发现了一个将相对href转换为绝对值的简单函数 from here
function qualifyURL(url) {
var a = document.createElement('a');
a.href = url;
return a.href;
}
并将其添加到我的代码中,选择所有标记并将其更改为绝对值:
var all_a = doc.getElementsByTagName("a");
for (var i = 0; i < all_a.length; i++) {
all_a[i].href = qualifyURL(all_a[i].href);
};