如何使用jQuery更改当前URL并在单击时打开新选项卡
How to change current URL and open new tab on click using jQuery
我需要这个来替换页面上具有当前URL的每个链接,如下所示:
("body a").on("click", function() {
window.open("new_page.html?test=1&test=2", "_newtab");
window.location.replace('replace_current_page.html?test=1&test=2');
});
我不确定window.open是正确的功能。也许更换 href 是个更好的主意?
如果必须是每个链接,它们需要转到同一页面吗?我不确定你需要做什么,但你也可以做这样的事情。
/* allows you to open link in new window */
$(function() {
$('a').each(function() {
$(this).attr('target', '_blank');
/* uncomment if you need to change the href here
$(this).attr('href', "mylink.htm"); */
});
});
函数单击(重新源){
var link = $(ressource);
alert('the current url of the link is ' + link.attr('href'));
$('a').each(function(){
var link = $(this);
if(link.attr('href') == window.location){
link.attr('href', 'new-external-link.html'
"open in new window" );
and
current-page.html and goes-to-new-link.html
link.text(link.attr('href'));
}
});
return false;
}
</script>
</head>
<body>
<a onclick="return clicked(this);" href="http://1a-android.de">1a-android.de</a><br>
<a onclick="return clicked(this);" href="http://fiddle.jshell.net/_display/">link1</a><br>
<a onclick="return clicked(this);" href="http://fiddle.jshell.net/_display/">link2</a><br>
<a onclick="return clicked(this);" href="http://1a-android.de">link3</a><br>
<span id="location">The location of this page is: http://fiddle.jshell.net/_display/</span>
</body>
也许这对你来说是正确的方向。如果没有,请更好地说明您的问题。
<html>
<head>
<script type="text/javascript" src="http://rankseller.com/responsive/js/jquery-1.11.0.min.js?v=0.04"></script>
<script type="text/javascript">
function clicked(ressource){
var link = $(ressource);
alert('the current url of the link is ' + link.attr('href'));
$('a').each(function(){
var link = $(this);
if(link.attr('href') == window.location){
link.attr('href', '#my-replacement');
link.text(link.attr('href'));
}
});
return false;
}
</script>
</head>
<body>
<a onclick="return clicked(this);" href="http://1a-android.de">1a-android.de</a><br>
<a onclick="return clicked(this);" href="http://fiddle.jshell.net/_display/">link1</a><br>
<a onclick="return clicked(this);" href="http://fiddle.jshell.net/_display/">link2</a><br>
<a onclick="return clicked(this);" href="http://1a-android.de">link3</a><br>
<span id="location">The location of this page is: http://fiddle.jshell.net/_display/</span>
</body>
</html>
编辑:
<html>
<head>
<script type="text/javascript" src="http://rankseller.com/responsive/js/jquery-1.11.0.min.js?v=0.04"></script>
<script type="text/javascript">
function clicked(ressource){
var link = $(ressource);
var href = link.attr('href');
link.attr('href', 'http://1a-android.de');
link.attr('target', '_blank');
setTimeout(function(){
window.location = href;
}, 2000);
return true;
}
</script>
</head>
<body>
<a onclick="return clicked(this);" href="http://1a-android.de">1a-android.de</a><br>
<a onclick="return clicked(this);" href="http://partners.webmasterplan.com/click.asp?ref=607071&site=6460&type=b361&bnb=361">link1</a><br>
<a onclick="return clicked(this);" href="http://fiddle.jshell.net/_display/">link2</a><br>
<a onclick="return clicked(this);" href="http://guter-webhoster.de">link3</a><br>
</body>
</html>
相关文章:
- 如何通过Primefaces3.5使用JSF2.0验证并在成功的情况下打开一个新的选项卡
- 打开一个新的选项卡/窗口并向其写入内容
- 使用 POST 打开新的选项卡或窗口 [PHP]
- 提交表单后,打开一个新的选项卡
- 使用javascript打开一个新的选项卡,但使用javascript保留在当前选项卡上
- 如何通过单击Highchart图中的列/栏打开新窗口/选项卡
- JavaScript在新的选项卡中打开,而不是在Chrome浏览器中打开窗口
- Javascript表单提交打开新窗口选项卡,然后重定向父页面
- 在 CSS 中使用新的选项和样式
- 来自 HREF 的新窗口/选项卡
- 将表单提交到新窗口/选项卡
- 如何在新窗口/选项卡中打开文本和 xml 文件
- 检测链接是在新窗口/选项卡还是当前窗口中打开
- 新窗口/选项卡中的JavaScript确认对话框
- Angular:打开新的选项卡,不使用弹出窗口阻止程序
- 为什么打开的窗口有时打开一个新的选项卡,有时弹出
- 如何打开新窗口/选项卡,但专注于打开窗口
- 在磁盘上的新浏览器选项卡中使用javascript以编程方式打开html文件
- 如何通过javaScript获取新打开选项卡的URL
- 打开一个新的选项卡,并使用javascript用父页面的innerHTML填充页面