gpt4 book ai didi

javascript - 单击打印按钮时如何防止打开新选项卡

转载 作者:行者123 更新时间:2023-12-03 11:37:55 24 4
gpt4 key购买 nike

我已经创建了一个用于打印 html 表格的应用程序,该应用程序工作正常,但我现在面临的问题是,当我单击打印选项时,会显示一个包含其中显示数据的新选项卡。如何防止该选项卡打开和显示?

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();
})

<强> Working Demo

谁能告诉我这个问题的解决方案吗?

最佳答案

添加 _self 属性应使其在同一选项卡上打开:

window.open("www.youraddress.com","_self")

关于javascript - 单击打印按钮时如何防止打开新选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26402822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com