首頁  >  文章  >  web前端  >  如何從不同的網域將 CSS 加入 iFrame?

如何從不同的網域將 CSS 加入 iFrame?

Patricia Arquette
Patricia Arquette原創
2024-10-25 05:38:02128瀏覽

How to Add CSS to iFrames from a Different Domain?

向 iFrame 添加 CSS

如果 iframe 是從不同的領域加載的,直接應用 CSS 可能是一個挑戰。以下解決方案解決了此問題。

修改樣式表

基於解決方案,以下是加入CSS 的方法:

使用JavaScript :

<code class="js">var cssLink = document.createElement("link");
cssLink.href = "file://path/to/style.css";
cssLink.rel = "stylesheet";
cssLink.type = "text/css";
frames['iframe'].document.body.appendChild(cssLink);</code>
使用JavaScript :

使用JavaScript :

<code class="js">var $head = $("iframe").contents().find("head");
$head.append($("<link/>", { rel: "stylesheet", href: "file://path/to/style.css", type: "text/css" }));</code>

使用jQuery:

潛在的安全問題請注意安全問題在Safari 中禁用同源策略時可能會出現這種情況。應採取適當措施確保安全實施。

以上是如何從不同的網域將 CSS 加入 iFrame?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn