Javascript 如何使用jQuery更改当前URL并在单击时打开新选项卡
我需要用以下内容替换页面上具有当前URL的每个链接:Javascript 如何使用jQuery更改当前URL并在单击时打开新选项卡,javascript,jquery,replace,stringify,Javascript,Jquery,Replace,Stringify,我需要用以下内容替换页面上具有当前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
("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"); */
});
});
也许对你来说方向是对的。如果没有,请更好地说明您的问题
<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>
已单击函数(ressource){
var link=$(ressource);
警报('链接的当前url为'+link.attr('href'));
$('a')。每个(函数(){
var-link=$(这个);
if(link.attr('href')==window.location){
link.attr('href','我的替代者');
link.text(link.attr('href'));
}
});
返回false;
}
此页面的位置为:http://fiddle.jshell.net/_display/
编辑:
<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>
已单击函数(ressource){
var link=$(ressource);
var href=link.attr('href');
link.attr('href','http://1a-android.de');
link.attr('target','u blank');
setTimeout(函数(){
window.location=href;
}, 2000);
返回true;
}
已单击函数(ressource){
var link=$(ressource);
警报('链接的当前url为'+link.attr('href'));
$('a')。每个(函数(){
var-link=$(这个);
if(link.attr('href')==window.location){
link.attr('href','newexternallink.html'
“在新窗口中打开”);
及
current-page.html和goes-to-new-link.html
link.text(link.attr('href'));
}
});
返回false;
}
此页面的位置为:http://fiddle.jshell.net/_display/
感谢您的回复。是的,他们都必须改变到同一个链接,并打开一个新的链接。所有链接都具有相同的功能。@AdamSteinhauser如果此答案解决了您的问题,请单击左侧的复选标记接受。谁投票否决了我对此问题的支持,原因是什么?我试图用新url替换当前url,并在新窗口中加载新链接。。当前页面转到另一个页面…指定:“用新的url替换当前url”。。。。您可以从我的代码中使用var link=$(this);获取链接的url。window.open(link.attr('href'),“_newtab”);可能您必须添加www.youdomain.com/作为前缀。例如,如果页面上的当前链接是指定的新链接,则将覆盖该链接。。。我正在测试你的代码
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>