【问题标题】:how can i import an iframe html code from a string如何从字符串中导入 iframe html 代码
【发布时间】:2020-10-23 18:51:41
【问题描述】:
<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。

【问题讨论】:

  • 什么?我不明白

标签: javascript python html


【解决方案1】:

我为你的下面做了一个sn-p。根据您的问题,我不完全确定您在寻找什么,但请告诉我这是否适合您。

//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;
}
&lt;div id="container"&gt;&lt;/div&gt;

【讨论】:

    【解决方案2】:

    您可以使用 jquery 在 html() 函数中传递字符串

    例子:

    var x = "

    你好世界

    "

    $("#div_id").html(x);

    在你的情况下

    var string = '你的 iframe 代码为字符串'

    $("#container").html(string);

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-28
      • 2020-03-23
      • 1970-01-01
      • 2020-05-26
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多