单击该按钮后,由于站点动画,我需要页面等待.8s在同一窗口中打开链接。
下面的此超时代码仅在href目标为_blank
时才有效,而在目标为默认的_self
时则无效-链接变为undefined
。
$("#button").click(function() {
setTimeout(function(url) {
window.location = url;
}, 800, linkUrl);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<a href="https://www.w3schools.com"><button id="button">Click</button></a>
如何修复我的代码,以便在单击按钮后在同一窗口中打开新链接之前,它会等待.8s?
最佳答案
如果单击按钮,则触发锚href。只需删除a
标记并设置window.location
。
$("#button").click(function() {
setTimeout(function() {
window.location = "https://www.w3schools.com";
}, 800);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<button id="button">Click</button>