通过单击 jquery 中的链接打开新选项卡(窗口)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15153781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
open new tab(window) by clicking a link in jquery
提问by thkang
I have a web app that displays rows with go
and delete
buttons.
我有一个 Web 应用程序,它显示带有go
和delete
按钮的行。
If a user clicks go
, it should open new tab/window with url built from the row's data.
如果用户点击go
,它应该打开新的标签/窗口,其中 url 是根据行数据构建的。
how can I do this in jquery? What I'm doing is :
我怎样才能在 jquery 中做到这一点?我正在做的是:
$('.go').click( function () {
var wid = {{ wid|tojson|safe }};
var sid = $(this).prop('id');
url = $script_root + '/' + wid + '/' + sid;
// go to url
});
some update:
一些更新:
What I'm really tring to accomplish is dynamically update href
of an <a>
element.
我真正特林做到的是动态更新href
的的<a>
元素。
<a id="foo" href="#">foo</a>
<script type="text/javascript>
$('#foo').click( function() {
$(this).prop('href', 'http://www.google.com/');
});
</script>
which doesn't work (fiddle :http://jsfiddle.net/6eLjA/)
这不起作用(小提琴:http: //jsfiddle.net/6eLjA/)
回答by karaxuna
Try this:
尝试这个:
window.open(url, '_blank');
This will open in new tab (if your code is synchronous and in this case it is. in other case it would open a window)
这将在新选项卡中打开(如果您的代码是同步的,在这种情况下是同步的。在其他情况下它会打开一个窗口)