javascript - 如何从字符串中导入 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,上面显示了这个字符串代码。
解决方案
我为你的下面做了一个片段。根据您的问题,我不完全确定您在寻找什么,但如果这对您有用,请告诉我。
//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>
推荐阅读
- r - 如何使用ggplot2在直线和倒置geom_density()之间添加垂直空格
- ios - 在从初始化程序返回之前,不会在所有路径上调用“self.init”
- kotlin - 在 Kotlin 中选择并显示日期
- ios - 快速视频流启动
- c# - 导出到统一时,搅拌机模型不导出材料。请帮忙?
- maven - Maven surefire插件输出测试摘要xml?
- android - 辅助功能节点信息获取图像内容
- laravel - Laravel:使用 Trait 和 Scope 检查该字段是否在路由中填写
- go - Golang 多部分文件表单请求
- android - RxJava observeOn currentThread 调用 opengl 函数