javascript - 对于在 html 中使用 iframe 循环,Spotify 播放按钮
问题描述
我正在尝试制作一个 for 循环,为我放入特定列表中的每个 URI 显示 Spotify 播放按钮。因此,如果此专辑spotify:album:08Cq2FeFxRQdpkKrjCGrth
在列表中,它将显示为
<iframe src="https://open.spotify.com/embed/album/08Cq2FeFxRQdpkKrjCGrth" width="300" height="380" frameborder="0" allowtransparency="true" allow="encrypted-media"></iframe>
因此 for 循环会将 URI 放入iframe
标记中。
这是我的以下代码,我已经在网上查看了许多教程和示例,但无法弄清楚这种情况。
var list = ["spotify:album:08Cq2FeFxRQdpkKrjCGrth"];
for (var i = 0; i < list.length; i++) {
var source = list[i].split(":")[2];
var iframe = document.createElement("iframe");
iframe.src = source;
iframe.width = "300";
iframe.height = "380";
iframe.frameborder = "0";
iframe.allowtransparency = "true";
iframe.allow = "encrypted-media";
document.getElementById("demo").appendChild(iframe);
}
解决方案
这是我想出的。但是您需要将其部署到服务器才能工作,否则即使您添加了沙箱,无论如何您都会收到此错误。这需要使用正确的 http/https url 部署到服务器(没有本地或 ipaddress url 将起作用)
embed.2019-01-15_bcdc2b81.js:1 Uncaught DOMException: Failed to read the 'cookie' property from 'Document': The document is sandboxed and lacks the
var list = ["spotify:album:1DFixLWuPkv3KT3TnV35m3"];
var url = "https://embed.spotify.com?uri="
for (var i = 0; i < list.length; i++) {
var source =url + list[i]
var iframe = document.createElement("iframe");
iframe.src = source;
iframe.width = "300";
iframe.height = "380";
iframe.frameborder = "0";
iframe.allowtransparency = "true";
iframe.allow = "encrypted-media";
document.getElementById("demo").appendChild(iframe);
}
iframe{
border:1px solid red;
width:100%;
height:100%;
}
<div id="demo"></div>
推荐阅读
- python - 如何使用python检查ping?
- python - Odoo 13 PDF 如果记录数增加一些空白页
- algorithm - 从大小为 n 的整数的未排序数组构造二叉搜索树
- scala - spark udf 没有被调用
- flutter - 右侧的无限像素溢出的 RenderFlex。在容器内使用行时
- python - 未使用 Flask JWT 扩展设置 Cookie
- css - CSS / SCSS 不会在 Docker 中运行的 ruby on rails 中更新
- php - 数组值未传递给函数
- c# - ASP.Net 找不到其他项目的视图
- php - 我如何在 mod_rewrite 引擎的正则表达式中允许空间