使用jQuery编辑iframe-src

Edit iframe src using jQuery

本文关键字:iframe-src 编辑 jQuery 使用      更新时间:2023-09-26

为什么我的代码不粘贴我的iframe-src?它在HTML中只是空的。

<iframe id="Stream" src="" class="col-sm-12" frameborder="0" scrolling="no"></iframe>
<iframe id="Chat" src="" frameborder="0" scrolling="no"></iframe>
$('#Stream').attr("https://player.twitch.tv/?channel=" + channel, url)
$('#Chat').attr("https://www.twitch.tv/" + channel + "/chat?popout=", url)

调用attr()方法的语法不正确。第一个参数是属性的名称,第二个参数应该是要设置的值。在可能的情况下,也最好使用prop()方法,就像在这种情况下一样。

$('#Stream').prop('src', "https://player.twitch.tv/?channel=" + channel);
$('#Chat').prop('src', "https://www.twitch.tv/" + channel + "/chat?popout=");

attr()方法
prop()方法

请注意,如果请求的域不允许使用X-Frame-Options在第三方网站上的iframe中呈现,则这可能仍然无法正常工作。

另一种更改iframe源路径的方法。

请在下面查看。

$('#Stream').attr("src","https://player.twitch.tv/?channel=" + channel);
$('#Chat').attr("src","https://www.twitch.tv/" + channel + "/chat?popout=");