gpt4 book ai didi

javascript - 如何将下载重定向到新标签

转载 作者:行者123 更新时间:2023-11-28 02:29:53 25 4
gpt4 key购买 nike

下面的代码包含一个 HTML 选项卡,其中有一个图像。我目前正在使用库 HTML2Canvas 来捕获 div 并下载它。但问题是当我单击 capture 按钮时,它会立即下载图像。

我想要完成的是,当我单击 capture 按钮时,它将打开一个新选项卡并下载图像,然后关闭该选项卡。有没有一种简单的方法可以做到这一点?任何帮助将不胜感激谢谢。

function sendData() {
html2canvas(document.getElementById('capture'), {
allowTaint: false,
useCORS: true
}).then(function(canvas) {
$('#test').attr('href', canvas.toDataURL('image/png'));
$('#test').attr('download', 'Test.png');
$('#test')[0].click();
});
}


function openCity(evt, cityName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
}

document.getElementById("defaultOpen").click();
body {
font-family: Arial;
}

.tab {
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
margin-top: 10px;
border-top-left-radius: 8px;
border-top-right-radius: 8px;
}


/* Style the buttons inside the tab */

.tab button {
background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
font-size: 17px;
border-bottom: 8px;
}


/* Change background color of buttons on hover */

.tab button:hover {
background-color: #ddd;
}


/* Create an active/current tablink class */

.tab button.active {
background-color: #ccc;
}


/* Style the tab content */

.tabcontent {
display: none;
padding: 6px 25px;
border: 1px solid #ccc;
border-top: none;
-webkit-animation: fadeEffect 1s;
animation: fadeEffect 1s;
border-bottom-left-radius: 8px;
border-bottom-right-radius: 8px;
background-color: white;
}

.test-panel {
display: table;
max-height: 100%;
width: 85%;
background-color: #b7bcbe;
margin-left: auto;
margin-right: auto;
margin-top: 25px;
margin-bottom: 25px;
padding-bottom: 20px;
padding-top: 20px;
}

.tabwidth {
width: 85%;
margin: 0 auto;
}
<!DOCTYPE html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>

<head>
<meta charset="utf-8" />
<style>

</style>
<link rel="shortcut icon" href="//#" />
<script type="text/javascript" src="https://html2canvas.hertzen.com/dist/html2canvas.js"></script>
<script type="text/javascript" src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
</head>

<body>
<div id="capture">
<div class="test-panel">
<div class="tabwidth">
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'London')" id="defaultOpen">Pikachu</button>
</div>

<div id="London" class="tabcontent">
<img src="https://s22.postimg.cc/l2txqenox/Pikachu.png" width="300" height="300" crossorigin>
</div>


</div>
</div>
</div>
<button id="match-button" onclick="sendData();">capture</button>
<a id="test" href="#"></a>
</body>

</html>

<script>
</script>

最佳答案

尝试将 target 属性 blank 添加到 a 元素。它会在新选项卡中打开链接。

<a id="test" href="#" target="_blank"></a>

关于javascript - 如何将下载重定向到新标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51490876/

25 4 0