Javascript 打印函数返回错误elem.cloneNode不是函数
我有以下功能来打印表单,如下所示:Javascript 打印函数返回错误elem.cloneNode不是函数,javascript,jquery,Javascript,Jquery,我有以下功能来打印表单,如下所示: document.getElementById("btnPrint").onclick = function () { printElement(document.getElementsByClassName("printThis")); } function printElement(elem) { var domClone = elem.cloneNode(true);
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不是函数
在printElement
在HTMLButtonElement.document.getElementById.onclick
如果发送的是
节点列表
,而不是节点
,则应使用以下命令修改第二行:
printElement(document.getElementsByClassName("printThis")[0]);
如果发送的是
节点列表
,而不是节点
,则应使用以下命令修改第二行:
printElement(document.getElementsByClassName("printThis")[0]);