<span>Ways to to change CSS in an iframe </span><span>Some of these work, and some don't but i've included both for you to ponder amd make up your own decisions. </span> <span><span>This</span> method didn't work for me. </span><span>[js] </span><span>var text = 'hi'; </span><span>var content = "" + text + ""; </span> <span>var iframe = document.createElement("iframe"); </span>iframe<span>.src = ' document.body.appendChild(iframe); </span> <span>var doc = iframe.document; </span><span>if(iframe.contentDocument) </span>doc <span>= iframe.contentDocument; // For NS6 </span><span>else if(iframe.contentWindow) </span>doc <span>= iframe.contentWindow.document; // For IE5.5 and IE6 </span><span>// Put the content in the iframe </span>doc<span>.open(); </span>doc<span>.writeln(content); </span>doc<span>.close(); </span> doc<span>.getElementsByTagName("body").style.backgroundColor = 'red';</span>
<span>var cssLink = document.createElement("link") </span>cssLink<span>.href = "style.css"; </span>cssLink <span>.rel = "stylesheet"; </span>cssLink <span>.type = "text/css"; </span>frames<span>['frame1'].document.body.appendChild(cssLink);</span>
<span>var frm = frames['frame'].document; </span><span>var otherhead = frm.getElementsByTagName("head")[0]; </span><span>var link = frm.createElement("link"); </span>link<span>.setAttribute("rel", "stylesheet"); </span>link<span>.setAttribute("type", "text/css"); </span>link<span>.setAttribute("href", "style.css"); </span>otherhead<span>.appendChild(link);</span>
這是由於“相同原始策略”造成的,這是Web安全的關鍵方面。它可以防止一個頁面上的腳本訪問或修改另一個頁面的屬性。該策略的實施是為了防止潛在的惡意腳本在另一個網頁上訪問敏感數據。
>>
>
var iframedoc = iframe.contentDocument || iframe.contentwindow.document;
iframedoc.body.style.style.backgroundColor =“ red”;
>這也會將iframe身體的背景色更改為紅色。
>。 iframe可能來自另一個域,在這種情況下,由於相同的原始策略,您無法修改其CSS。或者,當您嘗試更改CSS時,IFRAME的內容可能未被充分加載。 To ensure the iFrame is fully loaded, you can use the load event.
Yes, if you have control over the iFrame’s source page, you can use the postMessage method to send messages from the parent page to the iFrame, and add a listener in the iFrame’s page to receive the messages and make the necessary更改。
$(“#myiframe”)。 on(“ load”,function(){
$(this).contents()。不,我可以更改iframe內容的CSS,不,您無法更改PDF文件的CSS。 CSS是一種樣式表語言,用於描述用HTML或XML編寫的文檔的外觀和格式。 PDF文件具有自己的格式,無法使用CSS進行樣式。
我可以使用CSS更改IFRAME的大小嗎?您可以做到這一點:
iframe {
寬度:500px;
iframe {
border:none;以上是使用jQuery在iframe內容上更改CSS的詳細內容。更多資訊請關注PHP中文網其他相關文章!