I have a button which navigate user to another page. (It has to be button not link). I want to open the page in new tab (or even new window!). Do I have to use JavaScript? or is there any other way! I'll appreciate your help.
我有一个按钮,用户导航到另一个页面。 (它必须是按钮而不是链接)。我想在新标签页面中打开页面(甚至是新窗口!)。我必须使用JavaScript吗?还是有别的办法!我将感激你的帮助。
Cheers.
5 个解决方案
#1
14
To expand on the answers suggesting using `target="_blank" in Rails...
扩展答案建议在Rails中使用`target =“_ blank”...
If you're using the button_to
helper, this is automatically wrapped in a form by Rails. It's the form
tag that needs this attribute. To get it, pass it as a value in the hash of html options, for example:
如果您正在使用button_to帮助程序,则会自动将其包装在Rails的表单中。这是需要此属性的表单标记。为了得到它,将它作为html选项的散列中的值传递,例如:
button_to t(:translated_text), foo_path(foo.id), class: 'btn btn-primary', form: {target: '_blank'}
the class:
and form:
are elements of the html_options
hash. The form:
option is special because it tell Rails you want to pass the HTML option to the form
tag, instead of the input
tag of the button itself.
class:和form:是html_options哈希的元素。 form:选项是特殊的,因为它告诉Rails你想要将HTML选项传递给form标签,而不是按钮本身的输入标签。
#2
0
This is rather an html question. If you want to use buttons instead of links, you can provide the target attribute to the form as described here: http://www.w3schools.com/tags/att_form_target.asp
这是一个html问题。如果要使用按钮而不是链接,可以按如下所述为表单提供目标属性:http://www.w3schools.com/tags/att_form_target.asp
#3
0
$('#btn').click(function(){
window.open('pageName');
})
#4
0
target="_blank"
is what you want, but it only works on forms and links.
target =“_ blank”是你想要的,但它只适用于表单和链接。
You can style a link to look like a button with css quite easily, which would be the cleanest way to achieve what you are after.
您可以非常轻松地将链接设置为具有css的按钮,这将是实现您所追求的最简洁的方法。
Unless your button is within a form, then you can just add target="_blank"
to your form and it should work.
除非您的按钮位于表单中,否则您只需将target =“_ blank”添加到表单中即可。
#5
0
With javascript, this selects all your buttons that have href attribute
使用javascript,这将选择具有href属性的所有按钮
$("button[href]").click(function() {
window.open($(this).attr('href'));
});
This actually opens a new window but I think when the browser forces it to open in new tab, it will open in a new tab.
这实际上打开了一个新窗口,但我认为当浏览器强制它在新选项卡中打开时,它将在新选项卡中打开。
Cheers!
#1
14
To expand on the answers suggesting using `target="_blank" in Rails...
扩展答案建议在Rails中使用`target =“_ blank”...
If you're using the button_to
helper, this is automatically wrapped in a form by Rails. It's the form
tag that needs this attribute. To get it, pass it as a value in the hash of html options, for example:
如果您正在使用button_to帮助程序,则会自动将其包装在Rails的表单中。这是需要此属性的表单标记。为了得到它,将它作为html选项的散列中的值传递,例如:
button_to t(:translated_text), foo_path(foo.id), class: 'btn btn-primary', form: {target: '_blank'}
the class:
and form:
are elements of the html_options
hash. The form:
option is special because it tell Rails you want to pass the HTML option to the form
tag, instead of the input
tag of the button itself.
class:和form:是html_options哈希的元素。 form:选项是特殊的,因为它告诉Rails你想要将HTML选项传递给form标签,而不是按钮本身的输入标签。
#2
0
This is rather an html question. If you want to use buttons instead of links, you can provide the target attribute to the form as described here: http://www.w3schools.com/tags/att_form_target.asp
这是一个html问题。如果要使用按钮而不是链接,可以按如下所述为表单提供目标属性:http://www.w3schools.com/tags/att_form_target.asp
#3
0
$('#btn').click(function(){
window.open('pageName');
})
#4
0
target="_blank"
is what you want, but it only works on forms and links.
target =“_ blank”是你想要的,但它只适用于表单和链接。
You can style a link to look like a button with css quite easily, which would be the cleanest way to achieve what you are after.
您可以非常轻松地将链接设置为具有css的按钮,这将是实现您所追求的最简洁的方法。
Unless your button is within a form, then you can just add target="_blank"
to your form and it should work.
除非您的按钮位于表单中,否则您只需将target =“_ blank”添加到表单中即可。
#5
0
With javascript, this selects all your buttons that have href attribute
使用javascript,这将选择具有href属性的所有按钮
$("button[href]").click(function() {
window.open($(this).attr('href'));
});
This actually opens a new window but I think when the browser forces it to open in new tab, it will open in a new tab.
这实际上打开了一个新窗口,但我认为当浏览器强制它在新选项卡中打开时,它将在新选项卡中打开。
Cheers!