【问题标题】:jQuery iframe not able to set youtube urljQuery iframe 无法设置 youtube 网址
【发布时间】:2015-12-28 11:51:43
【问题描述】:

我正在努力

这是我的 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?

请帮忙。

【问题讨论】:

标签: javascript php jquery iframe youtube


【解决方案1】:

试试这个

<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 = 正常

HTTPS -> HTTP = ERROR (X-Frame-Options' to 'SAMEORIGIN)

【讨论】:

  • 什么也没发生 :(
  • http协议,https安全。
  • &lt;iframe class="embed-responsive-item" id="placeYoutubeVideo" src="#" allowfullscreen&gt;&lt;/iframe&gt; &lt;script&gt; $(document).ready(function () { var neww = 'http://www.youtube.com/embed/NOubzHCUt48'; $('#placeYoutubeVideo').attr('src', neww); }); &lt;/script&gt; 它的工作......
  • @DmitriyKlimov 它的工作你应该把它作为答案发布
  • @DmitriyKlimov 它的工作.. wowwww 你能把这个作为答案发布吗,我在你的答案中有一些澄清
【解决方案2】:

相反,您可以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 );

【讨论】:

  • 谢谢,但我收到了这个错误 Uncaught TypeError: $(...).replace is not a function
  • 同样的错误现在'拒绝在框架中显示'youtube.com/watch?v=NOubzHCUt48&feature=youtu.be',因为它将'X-Frame-Options'设置为'SAMEORIGIN'。'
【解决方案3】:

试试这个

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() Method


编辑 01

如果您只想将其添加到一个元素中

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

【讨论】:

  • 我应该用 placeYoutubeVideo 替换 src 吗?
  • 或者我怎么知道它应该显示在 div placeYoutubeVideo
  • 删除src和&lt;iframe&gt;
  • 只有一个视频吗??或
  • 只有一个视频
【解决方案4】:

X-Frame-Options: Sameorigin 被 YouTube 用来阻止 clickjacking。您将需要嵌入实际的播放列表。

Embed videos & playlists

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-17
    • 2011-03-03
    • 2011-12-05
    • 2021-09-22
    • 2012-01-02
    • 2013-04-24
    • 1970-01-01
    • 2023-02-06
    相关资源
    最近更新 更多