Answers:
您可以喜欢:
window.open('url', 'window name', 'window settings')
jQuery的:
$('a#link_id').click(function(){
window.open('url', 'window name', 'window settings');
return false;
});
您也可以将设置target为_blank。
以下是在点击处理程序中强制目标的方法:
$('a#link_id').click(function() {
$(this).attr('target', '_blank');
});
$(this).attr('target', '_blank'); 可以更改为this.target = "_blank";此外,如果可以将页面上的锚点链接修改为具有rel="external"属性,则可以使用jQuery选择器为页面创建全局点击处理程序,a[rel="external"]而不用使用每个链接选择的点击处理程序与a#link_id
您将需要使用 window.open(url);
参考:
http : //www.htmlcodetutorial.com/linking/linking_famsupp_120.html
http://www.w3schools.com/jsref/met_win_open.asp
我刚刚找到了解决这个问题的有趣方法。我正在创建跨度,其中包含基于Web服务返回的信息。我考虑过尝试在跨度上放置一个链接,以便单击该链接时,“ a”将捕获点击。
但是我试图用跨度捕获点击...所以我想为什么在创建跨度时不这样做。
var span = $('<span id="something" data-href="'+url+'" />');
然后,我将点击处理程序绑定到跨度,该跨度基于'data-href'属性创建了一个链接:
span.click(function(e) {
e.stopPropagation();
var href = $(this).attr('data-href');
var link = $('<a href="http://' + href + '" />');
link.attr('target', '_blank');
window.open(link.attr('href'));
});
这成功地使我单击了一个范围并打开了一个具有正确URL的新窗口。
这有什么错<a href="myurl.html" target="_blank">My Link</a>?不需要Javascript ...
此解决方案还考虑了url为空而禁用(灰色)为空链接的情况。
$(function() {
changeAnchor();
});
function changeAnchor() {
$("a[name$='aWebsiteUrl']").each(function() { // you can write your selector here
$(this).css("background", "none");
$(this).css("font-weight", "normal");
var url = $(this).attr('href').trim();
if (url == " " || url == "") { //disable empty link
$(this).attr("class", "disabled");
$(this).attr("href", "javascript:void(0)");
} else {
$(this).attr("target", "_blank");// HERE set the non-empty links, open in new window
}
});
}
a.disabled {
text-decoration: none;
pointer-events: none;
cursor: default;
color: grey;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<a name="aWebsiteUrl" href="http://www.baidu.com" class='#'>[website]</a>
<a name="aWebsiteUrl" href=" " class='#'>[website]</a>
<a name="aWebsiteUrl" href="http://www.alibaba.com" class='#'>[website]</a>
<a name="aWebsiteUrl" href="http://www.qq.com" class='#'>[website]</a>
这不是一个很好的解决方法,但可以:
CSS:
.new-tab-opener
{
display: none;
}
HTML:
<a data-href="http://www.google.com/" href="javascript:">Click here</a>
<form class="new-tab-opener" method="get" target="_blank"></form>
Javascript:
$('a').on('click', function (e) {
var f = $('.new-tab-opener');
f.attr('action', $(this).attr('data-href'));
f.submit();
});
Microsoft IE不支持将名称用作第二个参数。
window.open('url', 'window name', 'window settings');
问题是window name。这将起作用:
window.open('url', '', 'window settings')
如果只使用该参数,Microsoft仅允许使用以下参数: