이 게시물에서는 다른 도메인에서 iframed 컨텐츠의 CSS를 변경하려는 시도에 대한 실패한 시도에 대한 문서도 포함했습니다. 또한 동일한 도메인의 iframe에서 CSS를 변경하는 방법에 대한 정보는 (내가 아는 한) 프록시 패스 나 그와 같은 경우에 수행 할 수 없습니다.
<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>불행히도 위에서 언급 한 보안상의 이유로 인해 CSS를 수정하는 직접적인 방법은 없습니다. 그러나 iframe의 소스 페이지를 제어 할 수있는 경우 부모 페이지에서 메시지를 받기 위해 스크립트를 추가하고 필요한 변경 사항을 수행 할 수 있습니다.
<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>iframe이 동일한 도메인에서 나온 경우 iframe의 CS를 변경하는 방법은 무엇입니까?
왜 내 CSS가 iframe에서 반영 하는가? IFRAME는 다른 도메인에서 나올 수 있으며,이 경우 동일한 오리핀 정책으로 인해 CSS를 수정할 수 없습니다. 또는 CSS를 변경하려고 할 때 Iframe의 콘텐츠가 완전히로드되지 않았을 수 있습니다. iframe이 완전히로드되도록하려면로드 이벤트를 사용할 수 있습니다.
PostMessage 메소드를 사용하여 크로스 도메인 iframe의 CSS를 변경할 수 있습니까?
예, Iframe의 소스 페이지를 제어 할 수 있다면 Postmessage 메소드를 사용하여 IFRAME로 메시지를 보내고 IFRAME의 메시지를 추가 할 수 있습니다. 변경.
$ ( "#myiframe"). on ( "load", function () .contents (). find ( "body"). css ( "background-color", "red");
});CSS를 사용하여 iframe 크기를 변경할 수 있습니까?
위 내용은 JQuery를 사용하여 iframe 컨텐츠에서 CSS를 변경하십시오의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!