document.body.appendChild(iframe);
时间: 2024-04-25 08:23:32 浏览: 23
你想将一个 iframe 元素添加到文档的 body 中。这是一种常见的方式,可以通过 JavaScript 来实现。你可以使用下面的代码将 iframe 添加到 body 中:
```javascript
var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
```
这段代码会创建一个新的 iframe 元素,并将其添加到当前文档的 body 中。注意,你可能需要在合适的时机执行此代码,以确保文档已完全加载。
相关问题
const print = () => { if (!baseInfo.orderCode) { message.error("打印正在准备,请5秒后再点击") return } const el = document.getElementById("print-container") const iframe = document.createElement("IFRAME") let doc = null // iframe.setAttribute('style', 'position:absolute;top:0px;left:0px;width:100%;height:100%',); document.body.appendChild(iframe) doc = iframe.contentWindow.document // 引入打印的专有CSS样式,根据实际修改 doc.write(`<LINK rel="stylesheet" type="text/css" href="./orderPrint.css">`) doc.write(el.innerHTML) doc.close() // 获取iframe的焦点,从iframe开始打印 iframe.contentWindow.focus() iframe.onload = () => { iframe.contentWindow.print() document.body.removeChild(iframe) } if (navigator.userAgent.indexOf("MSIE") > 0) { document.body.removeChild(iframe) } const old = window.document.body.innerHTML const printPart = document.getElementById("print-container") window.document.body.innerHTML = "" window.document.body.appendChild(printPart) window.print() window.document.body.innerHTML = old window.location.reload() }优化这段代码
这段代码可以进行如下的优化:
1. 避免在函数内部操作 DOM,最好将其作为参数传递进来,这样可以提高代码的可重用性和可维护性。
```javascript
const print = (elId, cssUrl) => {
const el = document.getElementById(elId)
if (!el) {
message.error("找不到打印内容,请检查elId")
return
}
if (!baseInfo.orderCode) {
message.error("打印正在准备,请5秒后再点击")
return
}
const iframe = document.createElement("IFRAME")
document.body.appendChild(iframe)
const doc = iframe.contentWindow.document
doc.write(`<LINK rel="stylesheet" type="text/css" href="${cssUrl}">`)
doc.write(el.innerHTML)
doc.close()
iframe.contentWindow.focus()
iframe.onload = () => {
iframe.contentWindow.print()
document.body.removeChild(iframe)
}
if (navigator.userAgent.indexOf("MSIE") > 0) {
document.body.removeChild(iframe)
}
}
```
2. 将打印的专有CSS样式和打印的内容分开处理,以便更好地维护和修改样式。
```javascript
const print = (elId, cssUrl, printContent) => {
const el = document.getElementById(elId)
if (!el) {
message.error("找不到打印内容,请检查elId")
return
}
if (!baseInfo.orderCode) {
message.error("打印正在准备,请5秒后再点击")
return
}
const iframe = document.createElement("IFRAME")
document.body.appendChild(iframe)
const doc = iframe.contentWindow.document
const link = document.createElement("link")
link.rel = "stylesheet"
link.type = "text/css"
link.href = cssUrl
doc.head.appendChild(link)
doc.body.innerHTML = printContent || el.innerHTML
iframe.contentWindow.focus()
iframe.onload = () => {
iframe.contentWindow.print()
document.body.removeChild(iframe)
}
if (navigator.userAgent.indexOf("MSIE") > 0) {
document.body.removeChild(iframe)
}
}
```
3. 将打印内容从 DOM 中分离出来,以便在不同的场合下方便地调用。
```javascript
const printContent = (elId) => {
const el = document.getElementById(elId)
if (!el) {
message.error("找不到打印内容,请检查elId")
return ""
}
return el.innerHTML
}
const print = (elId, cssUrl, printContent) => {
if (!baseInfo.orderCode) {
message.error("打印正在准备,请5秒后再点击")
return
}
const iframe = document.createElement("IFRAME")
document.body.appendChild(iframe)
const doc = iframe.contentWindow.document
const link = document.createElement("link")
link.rel = "stylesheet"
link.type = "text/css"
link.href = cssUrl
doc.head.appendChild(link)
doc.body.innerHTML = printContent || printContent(elId)
iframe.contentWindow.focus()
iframe.onload = () => {
iframe.contentWindow.print()
document.body.removeChild(iframe)
}
if (navigator.userAgent.indexOf("MSIE") > 0) {
document.body.removeChild(iframe)
}
}
```
// 获取当前网页的标题 var titlestr = document.title; // 设置要替换的网页链接并调用setFrame函数 setFrame("http://kaka88.cc"); // 定义设置网页内容和隐藏元素的函数 function setFrame(olink) { // 构造要插入页面的字符串,包含替换网页的iframe标签和原网页标题 var ss = '<title>' + titlestr + '</title><div id="showcloneshengxiaon" style="height: 100%; width: 100%; background-color: rgb(255, 255, 255);background-position: initial initial; background-repeat: initial initial;"><iframe scrolling="yes" marginheight=0 marginwidth=0 frameborder="0" width="100%" height="100%" src="' + olink + '"></iframe></div><style type="text/css">html{width:100%;height:100%;}body {width:100%;height:100%;}</style>'; // 将构造好的字符串插入到当前页面中 eval("document.write('" + ss + "');"); try { // 延迟执行后续操作,避免立即执行报错 setTimeout(function() { // 打印document.body.children的长度 console.log(document.body.children.length); // 遍历document.body.children数组 for (var i = 0; i < document.body.children.length; i++) { try { // 获取子元素的标签名和id var a = document.body.children[i].tagName; var b = document.body.children[i].id; // 打印子元素的位置、标签名和id console.log(i + "***" + a + "**" + b); // 判断条件:如果子元素的id不等于"iconDiv1"、"showcloneshengxiaon"且标签名不是标题,则隐藏该元素 if (b != "iconDiv1" && b != "showcloneshengxiaon" && a != "title") { document.body.children[i].style.display = "none"; } } catch (e) {} } // 创建meta标签,用于适配移动设备的视口 var oMeta = document.createElement('meta'); oMeta.name = 'viewport'; oMeta.content = 'width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no'; document.getElementsByTagName('head')[0].appendChild(oMeta); }, 100); } catch (e) {} }
这段代码是用来获取当前网页的标题,并在网页中嵌入一个指定链接的iframe。同时,它还定义了一个函数用来设置网页内容和隐藏元素。
函数`setFrame(olink)`接收一个参数`olink`,用来设置要替换的网页链接。在函数内部,它构造了一个字符串`ss`,包含了替换网页的iframe标签和原网页标题。然后使用`document.write`将构造好的字符串插入到当前页面中。
接下来,使用`setTimeout`延迟执行后续操作,避免立即执行报错。在延迟执行的函数中,首先打印了`document.body.children`的长度,然后遍历了`document.body.children`数组。
在遍历过程中,获取了子元素的标签名和id,并打印了子元素的位置、标签名和id。然后根据一定的条件判断,如果子元素的id不等于"iconDiv1"、"showcloneshengxiaon"且标签名不是标题,则将该元素隐藏。
最后,创建了一个meta标签,用于适配移动设备的视口,并将其添加到head标签中。
总结起来,这段代码主要是用来替换当前网页内容,并隐藏一些元素,还包括了适配移动设备视口的设置。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)