This question already has an answer here:
这个问题在这里已有答案:
- Open a URL in a new tab (and not a new window) using JavaScript 26 answers
- 使用JavaScript 26答案在新选项卡(而不是新窗口)中打开URL
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 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/)
1 个解决方案
#1
85
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)
这将在新选项卡中打开(如果您的代码是同步的,在这种情况下它是。在其他情况下它会打开一个窗口)
#1
85
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)
这将在新选项卡中打开(如果您的代码是同步的,在这种情况下它是。在其他情况下它会打开一个窗口)