<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中文网其他相关文章!