【发布时间】:2021-03-04 07:48:07
【问题描述】:
我使用了几种方法将样式添加到 iframe 文档的正文中并且它有效,问题是当我将 youtube 视频放入 iframe src 时,在这种情况下它不会让我向 iframe 文档添加样式. youtube是否阻止了添加?我的代码是:
<body>
<iframe id="if" src="https://www.youtube.com/embed/tgbNymZ7vqY?autoplay=1&mute=1" width="500px" height="500px"></iframe>
</div>
<script>
$("#if").on("load", function() {
let head = $("#if").contents().find("head");
let css = "<style type='text/css'> body { background-color: red; }</style>";
$(head).append(css);
});
</script>
【问题讨论】:
-
你有没有看过 jquery 给你的头部?根据 jquery 文档“contents() 方法也可以访问 iframe 的 HTML,如果它在同一个域中”而你的不是。