gpt4 book ai didi

javascript - 如何使用 jquery 修改 iframe 标签中的 url 协议(protocol)

转载 作者:行者123 更新时间:2023-11-30 20:32:56 30 4
gpt4 key购买 nike

我需要使用 jquery 将通过 iframe 标签加载的 youtube 视频的“http”协议(protocol)更改为“https”协议(protocol)。

这是我目前的代码

$(document).ready(function(){
var protocol = "http";
var url = $('body').find('iframe').attr('src');
if (protocol.test(url)) { // if we find http in src frame's atribute
//then I replace just that part, for "https"
}
});

有什么想法吗?

最佳答案

您只需要测试 http:// 是否包含在 iframe 源 URL 中。如果它包含在源 URL 中,则将 http:// 替换为 https:// 并设置新的 src URL。

$(document).ready(function(){
var iframe = $('body').find('iframe');
var url = iframe.attr('src');
if (url.includes("http://")) {
iframe.attr('src', url.replace("http://", "https://"));
}
});

关于javascript - 如何使用 jquery 修改 iframe 标签中的 url 协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50144052/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com