How to create an HTML button that acts like a link

2018-12-31 00:48发布

I would like to create an HTML button that acts like a link. So, when you click the button, it redirects to a page. I would like it to be as accessible as possible.

I would also like it so there aren't any extra characters, or parameters in the URL.

How can I achieve this?


Based on the answers posted so far, I am currently doing this:

<form method="get" action="/page2">
    <button type="submit">Continue</button>
</form>

but the problem with this is that in Safari and Internet Explorer, it adds a question mark character to the end of the URL. I need to find a solution that doesn't add any characters to the end of the URL.

There are two other solutions to do this: Using JavaScript or styling a link to look like a button.

Using JavaScript:

<button onclick="window.location.href='/page2'">Continue</button>

But this obviously requires JavaScript, and for that reason it is less accessible to screen readers. The point of a link is to go to another page. So trying to make a button act like a link is the wrong solution. My suggestion is that you should use a link and style it to look like a button.

<a href="/link/to/page2">Continue</a>

34条回答
孤独寂梦人
2楼-- · 2018-12-31 01:16

If what you need is that it will look like a button, with emphasis on the gradient image, you can do this:

<a href="www.yourlink.com" class="btn btn-gradient"><i class="fa fa-home"> Button Text</i></a>
查看更多
余生无你
3楼-- · 2018-12-31 01:17
| -main.html
查看更多
与风俱净
4楼-- · 2018-12-31 01:17

You could also set the buttons type-property to "button" (it makes it not submit the form), and then nest it inside a link (makes it redirect the user).

This way you could have another button in the same form that does submit the form, in case that's needed. I also think this is preferable in most cases over setting the form method and action to be a link (unless it's a search-form I guess...)

Example:

<form method="POST" action="/SomePath">
    <input type="text" name="somefield"/>
    <a href="www.target.com"><button type="button">Go to Target!</button></a>
    <button type="submit">submit form</button>
</form>

This way the first button redirects the user, while the second submits the form.

Be careful to make sure the button doesn't trigger any action, as that will result in a conflict. Also as Arius pointed out, you should be aware that, for the above reason, this isn't strictly speaking considered valid HTML, according to the standard. It does however work as expected in Firefox and Chrome, but I haven't yet tested it for Internet Explorer.

查看更多
永恒的永恒
5楼-- · 2018-12-31 01:18

You can simply put an a tag around the element:

<a href="http://google.com" target="_blank">
<button>My Button</button>
</a>

https://jsfiddle.net/hj6gob8b/

查看更多
浅入江南
6楼-- · 2018-12-31 01:19

If you are using an inside form, add the attribute type="reset" along with the button element. It will prevent the form action.

<button type="reset" onclick="location.href='http://www.example.com'">
    www.example.com
</button>
查看更多
查无此人
7楼-- · 2018-12-31 01:20

It is actualy very simple and without using any form elements. You can just use the <a> tag with a button inside :).

Like this:

<a href="http://www.google.com" target="_parent"><button>Click me !</button></a>

And it will load the href into the same page. Want a new page? Just use target="_blank".

查看更多
登录 后发表回答