如何将父窗口的所有样式表插入 iframe 的头部(相同域)?
我基于类似问题尝试的代码:
function () {
var d = frames[0].document;
var stylesheets = $("link").outerhtml;
d.open();
d.write(
'<html><head>'+
stylesheets +
'<style type="text/css">'+
'<\/style><\/head><body><\/body><\/html>'
);
d.close();
}
显然这在 IE 之外不起作用。提前致谢。
编辑:根据安东尼的回答尝试:
$("link[type='text/css']").each(function() {
var stylesheet = $(this).clone();
$("iframe").contents().find("head").append(stylesheet);
});
最佳答案
所选答案的一个问题是它使用了 .html()
,它只返回该元素的内部 html 内容,而不是元素本身。这是一个可行的解决方案:
$("link[type='text/css']").clone().appendTo($("iframe").contents().find("head"));
关于javascript - jquery 将所有样式表插入 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1490260/