CSS를 iframe에 적용하는 방법? RSS 링크를 표시하기 위해 iframe 섹션이있는

RSS 링크를 표시하기 위해 iframe 섹션이있는 간단한 페이지가 있습니다. 기본 페이지에서 iframe에 표시된 페이지에 동일한 CSS 형식을 적용하려면 어떻게해야합니까?



답변

편집 : 적절한 CORS 헤더 가 설정되어 있지 않으면 도메인 간 작동하지 않습니다 .

여기에는 iframe 블록 스타일과 iframe에 포함 된 페이지 스타일의 두 가지가 있습니다. 일반적인 방법으로 iframe 블록의 스타일을 설정할 수 있습니다.

<iframe name="iframe1" id="iframe1" src="empty.htm"
        frameborder="0" border="0" cellspacing="0"
        style="border-style: none;width: 100%; height: 120px;"></iframe>

iframe에 포함 된 페이지의 스타일은 하위 페이지에 포함시켜 설정해야합니다.

<link type="text/css" rel="Stylesheet" href="Style/simple.css" />

또는 자바 스크립트를 사용하여 상위 페이지에서로드 할 수 있습니다.

var cssLink = document.createElement("link");
cssLink.href = "style.css";
cssLink.rel = "stylesheet";
cssLink.type = "text/css";
frames['iframe1'].document.head.appendChild(cssLink);


답변

Google 캘린더 에서이 문제를 해결했습니다 . 어두운 배경에서 스타일을 지정하고 글꼴을 변경하고 싶었습니다.

다행히도 임베드 코드의 URL에는 직접 액세스에 대한 제한이 없었으므로 PHP 기능을 사용 file_get_contents하면 페이지에서 전체 컨텐츠를 가져올 수 있습니다. Google URL을 호출하는 대신 서버에있는 PHP 파일을 호출 할 수 있습니다 (예 : google.php여기에는 수정 된 원본 콘텐츠가 포함됩니다.

$content = file_get_contents('https://www.google.com/calendar/embed?src=%23contacts%40group.v.calendar.google.com&ctz=America/Montreal');

스타일 시트에 경로 추가하기 :

$content = str_replace('</head>','<link rel="stylesheet" href="http://www.yourwebsiteurl.com/google.css" /></head>', $content);

이렇게하면 스타일 시트가 head종료 태그 바로 앞에 배치됩니다 .

css와 js가 상대적으로 호출되는 경우 원래 URL에서 기본 URL을 지정하십시오.

$content = str_replace('</title>','</title><base href="https://www.google.com/calendar/" />', $content);

최종 google.php파일은 다음과 같아야합니다.

<?php
$content = file_get_contents('https://www.google.com/calendar/embed?src=%23contacts%40group.v.calendar.google.com&ctz=America/Montreal');
$content = str_replace('</title>','</title><base href="https://www.google.com/calendar/" />', $content);
$content = str_replace('</head>','<link rel="stylesheet" href="http://www.yourwebsiteurl.com/google.css" /></head>', $content);
echo $content;

그런 다음 iframe포함 코드를 다음과 같이 변경합니다 .

<iframe src="http://www.yourwebsiteurl.com/google.php" style="border: 0" width="800" height="600" frameborder="0" scrolling="no"></iframe>

행운을 빕니다!


답변

iframe의 컨텐츠가 완전히 제어되지 않거나 다른 스타일의 다른 페이지에서 컨텐츠에 액세스하려는 경우 JavaScript를 사용하여 조작 할 수 있습니다.

var frm = frames['frame'].document;
var otherhead = frm.getElementsByTagName("head")[0];
var link = frm.createElement("link");
link.setAttribute("rel", "stylesheet");
link.setAttribute("type", "text/css");
link.setAttribute("href", "style.css");
otherhead.appendChild(link);

사용하는 브라우저에 따라 동일한 도메인에서 제공되는 페이지에서만 작동 할 수 있습니다.


답변

var $head = $("#eFormIFrame").contents().find("head");

$head.append($("<link/>", {
    rel: "stylesheet",
    href: url,
    type: "text/css"
}));


답변

iframe은 대부분의 브라우저에서 다른 HTML 페이지처럼 보편적으로 처리됩니다. 동일한 스타일 시트를 iframe의 컨텐츠에 적용하려면 여기에 사용 된 페이지에서 해당 스타일 시트를 참조하십시오.


답변

다음은 <link>추가 스타일 시트를로드 하지 않고 CSS 코드를 직접 적용하는 방법 입니다.

var head = jQuery("#iframe").contents().find("head");
var css = '<style type="text/css">' +
          '#banner{display:none}; ' +
          '</style>';
jQuery(head).append(css);

iframe 페이지에서 배너를 숨 깁니다. 제안 해 주셔서 감사합니다!


답변

교수형에 따르면 iframe에서 페이지를 제어하는 ​​경우 가장 쉬운 방법은 일반적인 스타일로 공유 CSS 파일을 만든 다음 HTML 페이지에서 해당 파일에 연결하는 것입니다.

그렇지 않으면 iframe의 외부 페이지에서 페이지 스타일을 동적으로 변경할 수 없을 것입니다. 스푸핑 및 기타 해킹에 대한 오용으로 인해 브라우저가 크로스 프레임 DOM 스크립팅의 보안을 강화했기 때문입니다.

이 튜토리얼 은 일반적으로 iframe 스크립팅에 대한 자세한 정보를 제공합니다. 크로스 프레임 스크립팅 정보 는 IE 관점에서 보안 제한 사항을 설명합니다.