2013-11-29 86 views
0

我试图在点击链接后使用javascript将视频添加到div。这是我的一些代码,我不知道如果我这样做是正确的,因为我从来没有使用之前的JavaScript视频:使用javascript将视频添加到div

    var vid = document.createElement("source"); 
        vid.src="youtube link"; 
        vid.type="application/x-shock wave-flash"; 
        vid.style.width = "330px"; 
        vid.style.height = "200px"; 
       var x = document.getElementById('bottom'); 
        x.appendChild(vid); 

        <div id="bottom"></div> 
+0

它应该是vid.style.width = “330” + PX – MultiplyByZer0

+0

不知道这是否是你的问题。 – MultiplyByZer0

回答

1

我不认为这是一个使用YouTube视频的有效方式,因为你没有得到一个实际的视频链接。 YouTube已经看起来像这样的公开可用的嵌入链接:

<iframe width="560" height="315" src="//www.youtube.com/embed/9kd-PoHWah8" frameborder="0" allowfullscreen></iframe> 

您可以使用JavaScript来创建此。 Live demo here (click).

var iframe = document.createElement('iframe'); 
iframe.src = 'http://www.youtube.com/embed/9kd-PoHWah8'; 
iframe.width = '560'; 
iframe.height = '315'; 

var bottom = document.getElementById('bottom'); 
bottom.appendChild(iframe); 

进一步的信息和更多高级选项参见the iframe api docs here (click)