Javascript 如何创建一个链接,当单击该链接时,它会将您带到一个页面,并打开一个指向另一个url的新选项卡?
我希望有一个链接,单击该链接可执行两个操作:Javascript 如何创建一个链接,当单击该链接时,它会将您带到一个页面,并打开一个指向另一个url的新选项卡?,javascript,jquery,html,Javascript,Jquery,Html,我希望有一个链接,单击该链接可执行两个操作: 将浏览器的当前选项卡重定向到url A 打开一个新选项卡,将浏览器指向url B 我该怎么做?这个有HTML吗?我应该使用javascript吗?您需要使用javascript来实现这一点,大致如下: $('#foo').click(function(e) { e.preventDefault(); // stop the normal link behaviour so you can open a new window windo
我该怎么做?这个有HTML吗?我应该使用javascript吗?您需要使用javascript来实现这一点,大致如下:
$('#foo').click(function(e) {
e.preventDefault(); // stop the normal link behaviour so you can open a new window
window.open('http://foo.com/new-page'); // open new tab
window.location.assign($(this).prop('href')); // go to new page in current tab
});
您需要使用javascript来实现这一点,大致如下:
$('#foo').click(function(e) {
e.preventDefault(); // stop the normal link behaviour so you can open a new window
window.open('http://foo.com/new-page'); // open new tab
window.location.assign($(this).prop('href')); // go to new page in current tab
});
没有jQuery
<a href='#' onclick="f();return false;">link</a> // add onclick event
<script>
function f(){
document.location='/a.html'; // open in same tab
window.open('/b.html','_blank'); // open new tab
}
</script>
//添加onclick事件
函数f(){
document.location='/a.html';//在同一选项卡中打开
window.open('/b.html','u blank');//打开新选项卡
}
不带jQuery
<a href='#' onclick="f();return false;">link</a> // add onclick event
<script>
function f(){
document.location='/a.html'; // open in same tab
window.open('/b.html','_blank'); // open new tab
}
</script>
//添加onclick事件
函数f(){
document.location='/a.html';//在同一选项卡中打开
window.open('/b.html','u blank');//打开新选项卡
}