2016-07-20 3 views
0

내 웹 사이트에 맞춤 미리보기 이미지와 함께 YouTube 동영상을 삽입하려고합니다. 따라서 나는이 코드를 발견했다.onClick URL에 매개 변수를 추가하십시오.

<div onclick="this.nextElementSibling.style.display='block'; this.style.display='none'"> 
    <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=myImage" style="cursor:pointer" /> 
</div> 
<div style="display:none"> 
    <iframe width="420" height="315" src="https://www.youtube.com/embed/TlMNLFiARBA?rel=0&autoplay=1" frameborder="0" allowfullscreen></iframe> 
</div> 

그것은 지금까지 작동하지만, 자동 재생은 표지 이미지 뒤에 동영상을 재생합니다. autoplay 매개 변수 onClick을 추가하려고 했으므로 표지가 숨겨지면서 true로 변경됩니다.

나는

<div onclick="this.nextElementSibling.style.display='block';this.nextElementSibling.href + '?rel=0&autoplay=1'; this.style.display='none'"> 

를 사용하는 것을 시도했다하지만이 작동하지 않습니다. 솔직하게 말하면, 나는 JS에 대한 배경이 없으며 내가하고있는 일을 모른다. 아무도 이걸로 나를 도와 줄 수 없을까요? 주어진 링크 onClick에 URL 매개 변수를 추가하기 만하면됩니다.

감사합니다.

답변

0

나는 올바른 방법으로 일을하지 않는다고 생각합니다.

iframe의 url을 변경하면 iframe이 재생되기 시작합니다.

<div onclick="this.nextElementSibling.style.display='block';this.nextElementSibling.children[0].src='https://www.youtube.com/embed/TlMNLFiARBA?rel=0&autoplay=1'; this.style.display='none'"> 
    <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=myImage" style="cursor:pointer" /> 
</div> 
<div style="display:none"> 
    <iframe width="420" height="315" frameborder="0" allowfullscreen> 
    </iframe> 
</div> 

이 iframe을 표시하고 URL을 동시에 변경됩니다 :

이 시도.

관련 문제