Making a <button> that's a link in HTML

前端 未结 7 1169
野的像风
野的像风 2020-12-09 08:49

Basically, I like the way that is styled, with the clickable button when you add a little CSS. However, regular buttons are not st

相关标签:
7条回答
  • 2020-12-09 09:12

    A little bit easier and it looks exactly like the button in the form. Just use the input and wrap the anchor tag around it.

    <a href="#"><input type="button" value="Button Text"></a>
    
    0 讨论(0)
  • 2020-12-09 09:23

    You have three options:

    • Style links to look like buttons using CSS.

      Just look at the light blue "tags" under your question.

      It is possible, even to give them a depressed appearance when clicked (using pseudo-classes like :active), without any scripting. Lots of major sites, such as Google, are starting to make buttons out of CSS styles these days anyway, scripting or not.

    • Put a separate <form> element around each one.

      As you mentioned in the question. Easy and will definitely work without Javascript (or even CSS). But it adds a little extra code which may look untidy.

    • Rely on Javascript.

      Which is what you said you didn't want to do.

    0 讨论(0)
  • 2020-12-09 09:24

    <a id="reset-authenticator" asp-page="./ResetAuthenticator"><input type="button" class="btn btn-primary" value="Reset app" /></a>

    0 讨论(0)
  • 2020-12-09 09:27

    The 3 easiest ways IMHO are

    1: you create an image of a button and put a href around it. (Not a good way, you lose flexibility and will provide a lot of difficulties and problems.)

    2 (The easiest one) -> JQuery

    <input type="submit" someattribute="http://yoururl/index.php">
    
      $('button[type=submit] .default').click(function(){
         window.location = $(this).attr("someattribute");
         return false; //otherwise it will send a button submit to the server
    
       });  
    

    3 (also easy but I prefer previous one):

    <INPUT TYPE=BUTTON OnClick="somefunction("http://yoururl");return false" VALUE="somevalue">
    
    $fn.somefunction= function(url) {
        window.location = url;
    };
    
    0 讨论(0)
  • 2020-12-09 09:28
    <a href="#"><button>Link Text</button></a>
    

    You asked for a link that looks like a button, so use a link and a button :-) This will preserve default browser button styling. The button by itself does nothing, but clicking it activates its parent link.

    Demo:

    <a href="http://stackoverflow.com"><button>Link Text</button></a>

    0 讨论(0)
  • 2020-12-09 09:31

    Use javascript:

    <button onclick="window.location.href='/css_page.html'">CSS page</button>
    

    You can always style the button in css anyaways. Hope it helped!

    Good luck!

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