Javascript 如何从选定的html中删除节点,而不将其从当前页面中删除?
我正在使用jquery从我的页面抓取HTML,并在新窗口中打开它进行打印。我必须自定义一些样式,以便在打印时格式化。我的主页上有一个表,它有Javascript 如何从选定的html中删除节点,而不将其从当前页面中删除?,javascript,jquery,html,css,dom,Javascript,Jquery,Html,Css,Dom,我正在使用jquery从我的页面抓取HTML,并在新窗口中打开它进行打印。我必须自定义一些样式,以便在打印时格式化。我的主页上有一个表,它有来定义列的宽度,但是当我打印它时,我隐藏了最后一列(因为它有一个按钮,打印输出时不需要,并且该按钮在打印时会丢弃格式),但是列的宽度由于colgroup而保持不变。如果我从表中删除colgroup,则非隐藏列将填充宽度,并且格式是完美的。我不知道如何在我的打印窗口中删除colgroup节点,而不从原始窗口中删除它(这是不可能发生的)。这是我的打印代码: va
来定义列的宽度,但是当我打印它时,我隐藏了最后一列(因为它有一个按钮,打印输出时不需要,并且该按钮在打印时会丢弃格式),但是列的宽度由于colgroup而保持不变。如果我从表中删除colgroup,则非隐藏列将填充宽度,并且格式是完美的。我不知道如何在我的打印窗口中删除colgroup节点,而不从原始窗口中删除它(这是不可能发生的)。这是我的打印代码:
var newWin;
function printDetails() {
var content = $("#pdf-area").html();
var host = window.location.protocol + "//" + window.location.host;
var cssFiles = [
host + "/Themes/MSD/Styles/template/ma-order-details-2.css",
host + "/Scripts/libraries/zurb-responsive-tables/0.0.0/responsive-tables.css",
host + "/Themes/MSD/Styles/template/checkout-cart-2.css",
host + "/Themes/MSD/Styles/msdtheme.css",
host + "/Themes/MSD/Styles/foundation.css",
host + "/Themes/MSD/Styles/base.css",
host + "/Themes/MSD/Styles/print.css"
];
newWin = window.open();
cssFiles.forEach(function (file) {
newWin.document.write('<link rel="stylesheet" href="' + file + '" type="text/css" />');
});
newWin.document.write("<span class='please-wait'>Please wait...</span>");
newWin.document.write("<div class='ma-order-details-2'>");
newWin.document.write(content);
newWin.document.write("</div>");
newWin.document.close();
newWin.focus();
setTimeout(function () {
newWin.print();
newWin.close();
}, 1000);
}
var-newWin;
函数printDetails(){
var content=$(“#pdf区域”).html();
var host=window.location.protocol+“/”+window.location.host;
变量cssFiles=[
host+“/Themes/MSD/Styles/template/ma-order-details-2.css”,
host+“/Scripts/libraries/zurb responsive tables/0.0.0/responsive tables.css”,
主机+“/Themes/MSD/Styles/template/checkout-cart-2.css”,
主机+“/Themes/MSD/Styles/msdtheme.css”,
主机+“/Themes/MSD/Styles/foundation.css”,
主机+“/Themes/MSD/Styles/base.css”,
主机+“/Themes/MSD/Styles/print.css”
];
newWin=window.open();
forEach(函数(文件){
newWin.document.write(“”);
});
newWin.document.write(“请稍候…”);
newWin.document.write(“”);
newWin.document.write(内容);
newWin.document.write(“”);
newWin.document.close();
newWin.focus();
setTimeout(函数(){
newWin.print();
newWin.close();
}, 1000);
}
我想从内容中删除colgroup,或者从newWin中删除它,但我不知道如何执行此操作$(“colgroup”).remove()将从源代码页而不是新窗口中删除它。您应该执行以下操作:
var newPage = $('#newWin'); //Or whatever the new page will be
var content = $("#pdf-area").clone();
newPage.html(content.find('.colgroup').remove() );
诀窍是克隆内容,而不是获取原始内容并将其存储在变量中,然后您可以在不更改原始内容的情况下根据需要对其进行修改。您应该执行以下操作:
var newPage = $('#newWin'); //Or whatever the new page will be
var content = $("#pdf-area").clone();
newPage.html(content.find('.colgroup').remove() );
诀窍是克隆内容,而不是抓取原始内容并将其存储在变量中,然后您可以在不更改原始内容的情况下根据需要对其进行修改。您可以在打印样式表上使用
display:none
将其隐藏,而不是删除
@media print {
...
.colgroup {
display:none;
}
}
您可以在打印样式表上使用
display:none
隐藏它,而不是删除它
@media print {
...
.colgroup {
display:none;
}
}
这是最简单的解决办法!我不知道你能做到。那是最简单的解决办法!我不知道你能做到。