首页 > 解决方案 > 如何从字符串中导入 iframe html 代码

问题描述

<iframe width="560" height="315" allow="fullscreen; autoplay; encrypted-media" src="https://games.construct.net/174/latest" frameborder="0" allowfullscreen="true" msallowfullscreen="true" mozallowfullscreen="true" webkitallowfullscreen="true" allowpaymentrequest="false" referrerpolicy="unsafe-url" sandbox="allow-same-origin allow-forms allow-scripts allow-pointer-lock allow-orientation-lock allow-popups" scrolling="no"></iframe>

我将此源代码作为字符串。如何将其更改为 HTML 代码以运行?我尝试了 appendChiled 命令,但我不知道应该如何使用它。

我刚收到错误或空的 iframe,上面显示了这个字符串代码。

标签: javascriptpythonhtml

解决方案


我为你的下面做了一个片段。根据您的问题,我不完全确定您在寻找什么,但如果这对您有用,请告诉我。

//Set string as a variable
var string = '<iframe width="560" height="315" allow="fullscreen; autoplay; encrypted-media" src="https://games.construct.net/174/latest" frameborder="0" allowfullscreen="true" msallowfullscreen="true" mozallowfullscreen="true" webkitallowfullscreen="true" allowpaymentrequest="false" referrerpolicy="unsafe-url" sandbox="allow-same-origin allow-forms allow-scripts allow-pointer-lock allow-orientation-lock allow-popups" scrolling="no"></iframe>'

//Get container div by its id
var container = document.getElementById("container")

//Check if container has content, if there's nothing in the container then show the string
if (container.innerHTML.length < 1) {
container.innerHTML = string;
}
<div id="container"></div>


推荐阅读