jQuery iframe无法设置youtube url


jQuery iframe not able to set youtube url

我正在尝试进行

这是我的HTML 中的代码

<iframe class="embed-responsive-item"  id="placeYoutubeVideo"
        style="display:none" src="#" style="display:none" allowfullscreen></iframe>

在jQuery中,我试图更改视频的src

var neww = 'https://youtu.be/NOubzHCUt48';
        $('#placeYoutubeVideo').prop('src', neww)

但它抛出了以下错误:

拒绝显示'https://www.youtube.com/watch?v=NOubzHCUt48&feature=youtu.be'帧,因为它将"X-frame-Options"设置为"SAMEORIGIN"。

我该怎么解决这个问题?

如果不可能,是否可以通过点击功能生成带有youtube url的iframe?

请帮忙。

试试这个

<iframe class="embed-responsive-item" id="placeYoutubeVideo" src="#" allowfullscreen></iframe> 
<script> 
$(document).ready(function () { 
var neww = 'http://www.youtube.com/embed/NOubzHCUt48'; $('#placeYoutubeVideo').attr('src', neww); }); 
</script>

https://jsfiddle.net/vyt7gxwz/

HTTP->HTTPS=错误(X-Frame-Options到SAMEORIGIN)

HTTP->HTTP=OK

HTTPS->HTTPS=OK

HTTPS->HTTP=错误(X-Frame-Options到SAMEORIGIN)

相反,您可以replaceWith

 var neww = 'https://youtu.be/NOubzHCUt48';
 var newContent = '<iframe class="embed-responsive-item"  id="placeYoutubeVideo" style="display:none" src="'+neww +'" style="display:none"  allowfullscreen></iframe>'
// Replace 'neww ' with your new URL
     $('#placeYoutubeVideo').replaceWith(newContent );

试试这个

var neww = 'https://youtu.be/NOubzHCUt48';
iframe.setAttribute("src", neww);

语法

element.setAttribute(attributename,attributevalue)

参数值

attributename   |  String   |  Required. The name of the attribute you want to add
attributevalue  |  String   |  Required. The value of the attribute you want to add

HTML DOM setAttribute()方法


编辑01

如果你只想把它添加到一个元素中

document.getElementsByID("placeYoutubeVideo").setAttribute("src", neww); 

X-Frame-Options: Sameorigin被YouTube用来防止点击劫持。您需要嵌入实际播放列表。

嵌入视频&播放列表