我需要配置一个按钮,单击该按钮后,它将打开一个具有给定URL的新选项卡,但将重点放在原始选项卡上。
到目前为止,我有以下代码,该代码在新选项卡上打开url,但第二个选项卡被激活(而当我想要保持原始选项卡处于激活状态时)。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Testing Tabs</title>
<style type="text/css">
.invisible {
visibility: hidden;
}
.button_test {
margin-left: 200px;
margin-top: 100px;
font-size: 50px;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(function(){
$('.button_test').click(function(){
let url = 'https://www.google.com';
popupWindow(url);
});
});
let popupWindow = (url) => {
let $elem = $("<a>");
$elem
.attr('href', url)
.attr('target', '_blank')
.attr('class', 'invisible')
.html('Google')
;
$('body').append($elem);
$elem[0].click();
//*/
}
</script>
</head>
<body>
<button class="button_test">Click Here</button>
</body>
</html>
您在这里拥有JSFiddle
:https://jsfiddle.net/c75w9hsx
如果有任何建议,请相应地修改上面的JSFiddle
并在此处发布链接。
谢谢!