Javascript 你如何让 HTML 按钮表现得像一个超链接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2324169/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:46:56  来源:igfitidea点击:

How do you make an HTML button behave just like a hyperlink?

javascripthtmlonclick

提问by xarzu

How do you make an HTML button behave just like a hyperlink where, if you click on it, it will open a browser window showing a page you want?

你如何让 HTML 按钮表现得像一个超链接,如果你点击它,它会打开一个浏览器窗口,显示你想要的页面?

I understand this much. I think I will use this but instead of a link to some javascript code inside the quotes for "onclick" I want to put something simple that will launch a new browser window.

我明白这么多。我想我会使用这个,但不是在“onclick”的引号内链接到一些javascript代码,我想放一些简单的东西来启动一个新的浏览器窗口。

回答by YOU

onclick and window.open

onclick 和 window.open

<input type="button" onclick="window.open('http://www.example.com','_blank','resizable=yes')" />

回答by Ravia

I think this is the best solution for you.

我认为这对你来说是最好的解决方案。

Try this out

试试这个

<a href="http://www.stackoverlfow.com" target="_"><input 
type="button" value="Click Me"/></a>

Happy Coding!!

快乐编码!!

回答by Warty

In Head:  
<script>
openNewWindow = function()
{
 window.open(url, "_blank");
};
</script>
In Body:    
<input type="button" onclick="openNewWindow()" >

I prefer to define a function named openNewWindow() instead of putting the code in the input tag. This is for organization. I highly recommend you do this if you're planning on having many different buttons for opening different windows.

我更喜欢定义一个名为 openNewWindow() 的函数,而不是将代码放在输入标签中。这是为了组织。如果您打算使用许多不同的按钮来打开不同的窗口,我强烈建议您这样做。

回答by Kees de Kooter

You could do something like this:

你可以这样做:

window.open(url, "window-name", "menubar=no,innerWidth=600,innerHeight=600,toolbar=no,location=no,screenX=400,screenY=40");

Passing a name to the open method causes the browser to open a new window. The third argument defines the looks of the new window.

将名称传递给 open 方法会导致浏览器打开一个新窗口。第三个参数定义新窗口的外观。

回答by Josh Stodola

<input type="button" value="Google"
       onclick="window.open('http://www.google.com', '_blank');" />

回答by RalphTheWonderLlama

I'm 7 years late, I realize, but I had a similar issue and thought I comment for those who may follow.

我晚了 7 年,我意识到,但我有一个类似的问题,我想我为那些可能会关注的人发表评论。

If you're using Bootstrap, you can attach Bootstrap button classnames to anchor tags and make them look just like buttons:

如果您使用 Bootstrap,您可以将 Bootstrap 按钮类名附加到锚点标签,并使它们看起来像按钮:

<a href="path/file.ext" class="btn btn-md btn-link">I Look Like A Button</a>

<a href="path/file.ext" class="btn btn-md btn-link">I Look Like A Button</a>

Bootstrap supports basic sizes as well, including btn-smor btn-lg. Granted, not everyone uses Bootstrap but even then the default styles are free, easy to find, and can be copied into your own stylesheet even if you're using a custom boilerplate layout.

Bootstrap 也支持基本大小,包括btn-smbtn-lg。诚然,并非每个人都使用 Bootstrap,但即便如此,默认样式也是免费的,易于查找,并且即使您使用自定义样板布局也可以复制到您自己的样式表中。