Javascript 如何防止在单击“打印”按钮时打开新选项卡
我已经创建了一个用于打印html表的应用程序,该应用程序运行良好,但我现在面临的问题是,当我单击“打印”选项时,将显示一个新的选项卡,其中显示了数据。如何防止该选项卡打开和显示Javascript 如何防止在单击“打印”按钮时打开新选项卡,javascript,jquery,css-tables,Javascript,Jquery,Css Tables,我已经创建了一个用于打印html表的应用程序,该应用程序运行良好,但我现在面临的问题是,当我单击“打印”选项时,将显示一个新的选项卡,其中显示了数据。如何防止该选项卡打开和显示 function printData() { var divToPrint = document.getElementById("printTable"); var divToPrint2 = $(divToPrint).clone().find('th:first, td:first-child').rem
function printData()
{
var divToPrint = document.getElementById("printTable");
var divToPrint2 = $(divToPrint).clone().find('th:first, td:first-child').remove().end().prop('outerHTML');
newWin= window.open("");
var header = '<style>table th, table td {border-color: black;border-style: solid;border-width: 1px 1px 1px 1px;}table {border-collapse: collapse;}</style>';
divToPrint2=header+divToPrint2;
newWin.document.body.innerHTML = divToPrint2;
newWin.document.head.innerHTML = header;
newWin.print();
}
$('button').on('click',function(){
printData();
})
函数printData()
{
var divToPrint=document.getElementById(“printTable”);
var divToPrint2=$(divToPrint).clone().find('th:first,td:first child').remove().end().prop('outerHTML');
newWin=window.open(“”);
var header='表th,表td{边框颜色:黑色;边框样式:实心;边框宽度:1px 1px 1px;}表{边框折叠:折叠;}';
divToPrint2=标题+divToPrint2;
newWin.document.body.innerHTML=divToPrint2;
newWin.document.head.innerHTML=标题;
newWin.print();
}
$('button')。在('click',function()上{
printData();
})
有人能告诉我一些解决方法吗?添加“自我”属性将使其在同一选项卡上打开:
window.open("www.youraddress.com","_self")
在printData()
函数中创建新窗口。我认为这个功能是没有必要的。您可以编写打印css,例如:
@media print {
// css code will be here. You can hide buttons and table column or something here.
}
然后您可以调用window.print()
。
因此,我建议使用以下代码:
$("button").on("click", function(){
window.print();
})
使用方法如下
newWin= window.open("","_self");
而不是
newWin= window.open("");
您是否检查过该演示,当您单击“打印仍显示相同内容”时,“新建”选项卡未打开,但表内容仍显示“新建”选项卡未打开,但表内容仍显示良好,在这种情况下,您需要先存储当前HTML,进行打印,然后恢复到初始HTML。JSFIDLE不支持这一点。如果你仍然需要这个,请在这里发表评论。如果你能做到这一点,请接受答案。事实上,我希望这些功能,如删除操作列,再加上使用javascript应用样式