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:06

Why not just place your button inside of a reference tag e.g

<a href="https://www.google.com/"><button>Next</button></a>

This seems to work perfectly for me and does not add any %20 tags to the link, just how you want it. I have used a link to google to demonstrate.

You could of course wrap this in a form tag but it is not necessary.

When linking another local file just put it in the same folder and add the file name as the reference. Or specify the location of the file if in is not in the same folder.

<a href="myOtherFile"><button>Next</button></a>

This does not add any character onto the end of the URL either, however it does have the files project path as the url before ending with the name of the file. e.g

If my project structure was...

.. denotes a folder - denotes a file while four | denote a sub directory or file in parent folder

..public

查看更多
无色无味的生活
3楼-- · 2018-12-31 01:07
| ..html
查看更多
不再属于我。
4楼-- · 2018-12-31 01:09

People who have answered using <a></a> attributes on a <button></button> was helpful.

BUT then recently, I encountered a problem when I used a link inside a <form></form>.

The button is now regarded like/as a submit button (HTML5). I've tried working a way around, and have found this method.

Create a CSS style button like the one below:

.btn-style{
    border : solid 1px #0088cc;
    border-radius : 6px;
    moz-border-radius : 6px;
    -webkit-box-shadow : 0px 0px 2px rgba(0,0,0,1.0);
    -moz-box-shadow : 0px 0px 2px rgba(0,0,0,1.0);
    box-shadow : 0px 0px 2px rgba(0,0,0,1.0);
    font-size : 18px;
    color : #696869;
    padding : 1px 17px;
    background : #eeeeee;
    background : -webkit-gradient(linear, left top, left bottom, color-stop(0%,#eeeeee), color-stop(49%,#eeeeee), color-stop(72%,#cccccc), color-stop(100%,#eeeeee));
    background : -moz-linear-gradient(top, #eeeeee 0%, #eeeeee 49%, #cccccc 72%, #eeeeee 100%);
    background : -webkit-linear-gradient(top, #eeeeee 0%, #eeeeee 49%, #cccccc 72%, #eeeeee 100%);
    background : -o-linear-gradient(top, #eeeeee 0%, #eeeeee 49%, #cccccc 72%, #eeeeee 100%);
    background : -ms-linear-gradient(top, #eeeeee 0%, #eeeeee 49%, #cccccc 72%, #eeeeee 100%);
    background : linear-gradient(top, #eeeeee 0%, #eeeeee 49%, #cccccc 72%, #eeeeee 100%);
    filter : progid:DXImageTransform.Microsoft.gradient( startColorstr='#eeeeee', endColorstr='#eeeeee',GradientType=0 );

}

Or create a new one here : CSS Button Generator

And then create your link with a class tag named after the CSS style you have made:

<a href='link.php' class='btn-style'>Link</a>

Here's a fiddle:

JS Fiddle

查看更多
无色无味的生活
5楼-- · 2018-12-31 01:10

HTML

The plain HTML way is to put it in a <form> wherein you specify the desired target URL in the action attribute.

<form action="http://google.com">
    <input type="submit" value="Go to Google" />
</form>

If necessary, set CSS display: inline; on the form to keep it in the flow with the surrounding text. Instead of <input type="submit"> in above example, you can also use <button type="submit">. The only difference is that the <button> element allows children.

You'd intuitively expect to be able to use <button href="http://google.com"> analogous with the <a> element, but unfortunately no, this attribute does not exist according to HTML specification.

CSS

If CSS is allowed, simply use an <a> which you style to look like a button using among others the appearance property (only Internet Explorer support is currently (July 2015) still poor).

<a href="http://google.com" class="button">Go to Google</a>
a.button {
    -webkit-appearance: button;
    -moz-appearance: button;
    appearance: button;

    text-decoration: none;
    color: initial;
}

Or pick one of those many CSS libraries like Bootstrap.

<a href="http://google.com" class="btn btn-default">Go to Google</a>

JavaScript

If JavaScript is allowed, set the window.location.href.

<input type="button" onclick="location.href='http://google.com';" value="Go to Google" />
查看更多
倾城一夜雪
6楼-- · 2018-12-31 01:11

Another option is to create a link in the button:

<button type="button"><a href="yourlink.com">Link link</a></button>

Then use CSS to style the link and button, so that the link takes up the entire space within the button (so there's no miss-clicking by the user):

button, button a{position:relative;}
button a{top:0;left:0;bottom:0;right:0;}

I have created a demo here.

查看更多
梦寄多情
7楼-- · 2018-12-31 01:11
| -secondary.html

If I open main.html the URL would be,

http://localhost:0000/public/html/main.html?_ijt=i7ms4v9oa7blahblahblah

However, when I clicked the button inside main.html to change to secondary.html, the URL would be,

http://localhost:0000/public/html/secondary.html 

No special characters included at the end of the URL. I hope this helps. By the way - (%20 denotes a space in a URL its encoded and inserted in the place of them.)

Note: The localhost:0000 will obviously not be 0000 you'll have your own port number there.

Furthermore the ?_ijt=xxxxxxxxxxxxxx at the end off the main.html URL, x is determined by your own connection so obviously will not be equal to mine.


It might seem like I'm stating some really basic points but I just want to explain as best as I can. Thank you for reading and I hope this help someone at the very least. Happy programming.

查看更多
登录 后发表回答