javascript - 打印函数返回错误 elem.cloneNode 不是函数
问题描述
我有以下功能来打印表格,如下所示:
document.getElementById("btnPrint").onclick = function () {
printElement(document.getElementsByClassName("printThis"));
}
function printElement(elem) {
var domClone = elem.cloneNode(true);
var $printSection = document.getElementById("printSection");
if (!$printSection) {
var $printSection = document.createElement("div");
$printSection.id = "printSection";
document.body.appendChild($printSection);
}
$printSection.innerHTML = "";
$printSection.appendChild(domClone);
window.print();
}
当我运行该函数时,它返回以下错误:
未捕获的类型错误:elem.cloneNode 不是 HTMLButtonElement.document.getElementById.onclick 的 printElement 处的函数
解决方案
您发送的是 aNodeList
而不是 a Node
,您应该修改您的第二行:
printElement(document.getElementsByClassName("printThis")[0]);
推荐阅读
- javascript - 当值相同时,对象分配会覆盖先前的对象
- android - Android 应用内更新 API -> 立即更新问题
- java - 为什么在spring-boot中提供来自maven依赖的静态内容?
- go - 如何在 struct Beego 中设置唯一性
- javascript - 如何在对象中获取过滤后的结果
- java - 多线程对文件的操作
- javascript - 如何在 JS 表格中选择元素?
- python - 如何对精灵而不是屏幕进行 blit 和图像处理?
- reactjs - 是否可以在 Webstorm 中为反应组件显示不同的图标?
- javascript - 我如何循环思考循环并使用 ajax 在 django 中显示结果?