2014-08-31 2 views
1

YouTube iframe 소스 코드 위에 div를 배치하려고하지만 작동하지 않습니다. 아이디어가 있으십니까?youtube iframe 찾아서 div를 추가하십시오.

$('#iframe').prepend("<div style='width:800px; height:450px; background-color:#fff;'></div"); 

와 고전 유튜브은 iframe :

내 코드는

<iframe width="800" height="450" src="//www.youtube.com/embed/mllXxyHTzfg?rel=0" frameborder="0" allowfullscreen></iframe> 
+1

이것은 단지 가능하지 않습니다. – webkit

+0

iframe에 if와 id를주고 div에 div를 추가하더라도? –

+0

다른 도메인의 iframe 내부에는 아무런 영향을 미치지 않습니다. 실제로, 귀하가 귀하의 페이지에 올린 youtube iframe은 '귀하의 것'이 아닙니다. iframe과 그 요소를 스타일링하는 것만으로 iframe 콘텐츠 자체를 스타일링 할 수 있습니다 당신이 달성하고자하는 바를 정확히 설명한다면 어쩌면 다른 방법이있을 수 있습니다. – webkit

답변

1

자바 스크립트를 통해 iframe이 콘텐츠를 추가 할 수 없습니다.

div를 iframe 외부에 추가하고 CSS 배치를 사용하여 iframe 위에 div를 배치합니다.

3

당신은 자바 스크립트 솔루션을 원하는 경우 :

function coverVideo(){ 
    // Create an empty div 
    var cover = $("<div></div>").appendTo("body"); 
    cover.css({ 
     // Position directly over the iframe 
     position: "absolute", 
     top: $("#iframe").offset().top, 
     left: $("#iframe").offset().left, 

     // Style as you like 
     width: "800px", 
     height: "450px", 
     background: "#fff" 
    }); 
} 
+0

그것은 작동하지 않습니다 @ 리차드! –

+1

iframe에'id = "iframe"을 추가하고이 코드가 닫는 태그 바로 앞에 있는지 확인하십시오 (이 코드를 실행하기 전에 iframe이 존재하므로). –

+0

ID가 iframe에 적용되지 않았 음을 알 수 없습니다. 감사합니다. –

2

컨테이너 div의 내부에 iframe을 넣고 그 옆에 다른 요소를 추가 :

<div id="iframeContainer"> 
    <iframe width="800" height="450" src="//www.youtube.com/embed/mllXxyHTzfg?rel=0" frameborder="0" allowfullscreen></iframe> 
    <div class="overlay"></div> 
</div> 

위치를 오버레이 요소 :

#iframeContainer { 
    width: 800px; 
    height: 450px; 
    position: relative; 
} 

#iframeContainer iframe { 
    position: absolute; 
    top: 0; 
    left: 0; 
    right: 0; 
    bottom: 0; 
    z-index: 1; 
} 

#iframeContainer .overlay { 
    position: absolute; 
    top: 0; 
    left: 0; 
    right: 0; 
    bottom: 0; 
    z-index: 2; 
    background: #fff; 
    opacity: 0.7; 
} 

jsFiddle Demo.