How to create an HTML button that acts like a link?

后端 未结 30 3556
长发绾君心
长发绾君心 2020-11-21 04:18

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.

相关标签:
30条回答
  • 2020-11-21 05:14

    Also you can use a button:

    For example, in ASP.NET Core syntax:

    // Some other tags
     <form method="post">
          <input asp-for="YourModelPropertyOrYourMethodInputName"
          value="@TheValue" type="hidden" />
          <button type="submit" class="link-button" formaction="/TheDestinationController/TheDestinationActionMethod">
          @(TextValue)
          </button>
      </form>
    // Other tags...
    
    
    <style>
           .link-button {
            background: none !important;
            border: none;
            padding: 0 !important;
            color: #20a8d8;
            cursor: pointer;
        }
    </style>
    
    0 讨论(0)
  • 2020-11-21 05:15

    The only way to do this (except for BalusC's ingenious form idea!) is by adding a JavaScript onclick event to the button, which is not good for accessibility.

    Have you considered styling a normal link like a button? You can't achieve OS specific buttons that way, but it's still the best way IMO.

    0 讨论(0)
  • 2020-11-21 05:15

    Going along with what a few others have added, you can go wild with just using a simple CSS class with no PHP, no jQuery code, just simple HTML and CSS.

    Create a CSS class and add it to your anchor. The code is below.

    .button-link {
        height:60px;
        padding: 10px 15px;
        background: #4479BA;
        color: #FFF;
        -webkit-border-radius: 4px;
        -moz-border-radius: 4px;
        border-radius: 4px;
        border: solid 1px #20538D;
        text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.4);
        -webkit-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.4), 0 1px 1px rgba(0, 0, 0, 0.2);
        -moz-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.4), 0 1px 1px rgba(0, 0, 0, 0.2);
        box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.4), 0 1px 1px rgba(0, 0, 0, 0.2);
    }
    .button-link:hover {
        background: #356094;
        border: solid 1px #2A4E77;
        text-decoration: none;
    }
    
    <HTML>
        <a class="button-link" href="http://www.go-some-where.com"
           target="_blank">Press Here to Go</a>
    

    That is it. It is very easy to do and lets you be as creative as you'd like. You control the colors, the size, the shapes(radius), etc. For more detailsm, see the site I found this on.

    0 讨论(0)
  • 2020-11-21 05:16

    Use:

    <a href="http://www.stackoverflow.com/">
        <button>Click me</button>
    </a>
    

    Unfortunately, this markup is no longer valid in HTML5 and will neither validate nor always work as potentially expected. Use another approach.

    0 讨论(0)
  • 2020-11-21 05:17

    I know there have been a lot of answers submitted, but none of them seemed to really nail the problem. Here is my take at a solution:

    1. Use the <form method="get"> method that the OP is starting with. This works really well, but it sometimes appends a ? to the URL. The ? is the main problem.
    2. Use jQuery/JavaScript to do the link following when JavaScript is enabled so that ? doesn't end up appended to the URL. It will seamlessly fallback to the <form> method for the very small fraction of users who don't have JavaScript enabled.
    3. The JavaScript code uses event delegation so you can attach an event listener before the <form> or <button> even exist. I'm using jQuery in this example, because it is quick and easy, but it can be done in 'vanilla' JavaScript as well.
    4. The JavaScript code prevents the default action from happening and then follows the link given in the <form> action attribute.

    JSBin Example (code snippet can't follow links)

    // Listen for any clicks on an element in the document with the `link` class
    $(document).on('click', '.link', function(e) {
        // Prevent the default action (e.g. submit the form)
        e.preventDefault();
    
        // Get the URL specified in the form
        var url = e.target.parentElement.action;
        window.location = url;
    });
    <!DOCTYPE html>
    <html>
    
        <head>
            <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
            <meta charset="utf-8">
            <title>Form buttons as links</title>
        </head>
    
        <body>
            <!-- Set `action` to the URL you want the button to go to -->
            <form method="get" action="http://stackoverflow.com/questions/2906582/how-to-create-an-html-button-that-acts-like-a-link">
                <!-- Add the class `link` to the button for the event listener -->
                <button type="submit" class="link">Link</button>
            </form>
        </body>
    
    </html>

    0 讨论(0)
  • 2020-11-21 05:17

    For HTML 5 and styled button along with image background

    <a id="Navigate" href="http://www.google.com">
      <input 
        type="button"
        id="NavigateButton"
        style="
          background-image: url(http://cdn3.blogsdna.com/wp-content/uploads/2010/03/Windows-Phone-7-Series-Icons-Pack.png);
          background-repeat: no-repeat;
          background-position: -272px -112px;
          cursor:pointer;
          height: 40px;
          width: 40px;
          border-radius: 26px;
          border-style: solid;
          border-color:#000;
          border-width: 3px;" title="Navigate"
        />
    </a>

    0 讨论(0)
提交回复
热议问题