Is there a way to get a <button> element to link to a location without wrapping it in an <a

后端 未结 8 1958
礼貌的吻别
礼貌的吻别 2020-12-02 06:49

Just wondering if there is a way to get a HTML

相关标签:
8条回答
  • 2020-12-02 07:17

    Inline Javascript:

    <button onclick="window.location='http://www.example.com';">Visit Page Now</button>
    

    Defining a function in Javascript:

    <script>
        function visitPage(){
            window.location='http://www.example.com';
        }
    </script>
    <button onclick="visitPage();">Visit Page Now</button>
    

    or in Jquery

    <button id="some_id">Visit Page Now</button>
    
    $('#some_id').click(function() {
      window.location='http://www.example.com';
    });
    
    0 讨论(0)
  • 2020-12-02 07:17

    You can make it a non-submitting button (<button type="button">) and hook something like window.location = 'http://where.you.want/to/go' into its onclick handler. This does not work without javascript enabled though.

    Or you can make it a submit button, and do a redirect on the server, although this obviously requires some kind of server-side logic, but the upside is that is doesn't require javascript.

    (actually, forget the second solution - if you can't use a form, the submit button is out)

    0 讨论(0)
  • 2020-12-02 07:20

    Just do this

    <button OnClick=" location.href='link.html' ">Visit Page Now</button>
    

    Although, it's been a while since I've touched JavaScript - maybe location.href is outdated? Anyways, that's how I would do it.

    0 讨论(0)
  • 2020-12-02 07:24

    LINKS ARE TRICKY

    Consider the tricks that <a href> knows by default but javascript linking won't do for you. On a decent website, anything that wants to behave as a link should implement these features one way or another. Namely:

    • Ctrl+Click: opens link in new tab
      You can simulate this by using a window.open() with no position/size argument
    • Shift+Click: opens link in new window
      You can simulate this by window.open() with size and/or position specified
    • Alt+Click: download target
      People rarely use this one, but if you insist to simulate it, you'll need to write a special script on server side that responds with the proper download headers.

    EASY WAY OUT

    Now if you don't want to simulate all that behaviour, I suggest to use <a href> and style it like a button, since the button itself is roughly a shape and a hover effect. I think if it's not semantically important to only have "the button and nothing else", <a href> is the way of the samurai. And if you worry about semantics and readability, you can also replace the button element when your document is ready(). It's clear and safe.

    0 讨论(0)
  • 2020-12-02 07:25

    Well, for a link, there must be a link tag around. what you can also do is that make a css class for the button and assign that class to the link tag. like,

    #btn {
      background: url(https://image.flaticon.com/icons/png/128/149/149668.png) no-repeat 0 0;
      display: block;
      width: 128px;
      height: 128px;
      border: none;
      outline: none;
    }
    <a href="btnlink.html" id="btn"></a>

    0 讨论(0)
  • 2020-12-02 07:30

    Here it is using jQuery. See it in action at http://jsfiddle.net/sQnSZ/

    <button id="x">test</button>
    
    $('#x').click(function(){
        location.href='http://cnn.com'
    })
    
    0 讨论(0)
提交回复
热议问题