需要从父页面使用 javascript 将 css 样式添加到 iframe

Need to add css style to an iframe using javascript from parent page

本文关键字:css 样式 添加 iframe javascript      更新时间:2023-09-26

这是我到目前为止所拥有的:

var head = $target.prop("iframe").toLowerCase().contents().find("head");
var css = '<style type="text/css">' +
          'MY CSS IS HERE' +
          '</style>';
jQuery(head).append(css);

不幸的是,我有几个问题:

1)我无法通过javascript将jquery添加到页面(我尝试过,但它不起作用),所以我需要将其转换为javascript。

2)我确定代码有问题。请帮我修复错误。


注意:

我已经在这里尝试了所有内容,但它不起作用,特别是因为它与我需要的不同(iframe 没有 id 或类名,因此关于这个问题的所有内容都无关紧要)。

我认为如果你在同一个域下有iframe页面,你可以试试这个:

$("iframe").contents().find('head').append('<style type="text/css">' +'MY CSS IS HERE' + '</style>')