How do I make a link that goes nowhere?

前端 未结 7 753
梦毁少年i
梦毁少年i 2021-02-01 11:50

Is it possible to make a link that does nothing?

I want the item to be a link so I get the cursor to change on mouseover, but I don\'t want the link to actually go anywhe

相关标签:
7条回答
  • 2021-02-01 12:42

    Just add the style cursor:pointer to the element:

    <a id="clickme">Click Me</a>
    

    CSS:

    #clickme { cursor: pointer }
    

    Or (heaven forbid) in-line:

    <a style="cursor:pointer">Click Me</a>
    
    0 讨论(0)
  • 2021-02-01 12:45

    Will add to the browser history:

    <a href="#"></a>

    Won't add to the browser history (preferred method):

    <a href="javascript:;"></a>

    0 讨论(0)
  • 2021-02-01 12:51

    If you just want style, set the the CSS cursor property to pointer.

    But it sounds like you want <a href="javascript:void(do_something())">.

    0 讨论(0)
  • 2021-02-01 12:52

    I was looking for something simpler, and I think I found it. Do you want your link not to go anywhere? Omit the href.

    <a class='irrelevant_for_this_example'>Some text</a>

    href="#" will scroll to the top of the page.

    href="javascript:;" seems dodgy.

    A JavaScript solution seems unnecessary for something as simple.

    0 讨论(0)
  • 2021-02-01 12:53

    Here are two classic JavaScript approaches that won't give you an error in your JavaScript console and/or don't change the URL nor save it to your browser history:

    <a href="javascript:void(0);">Click on this JavaScript error-free link that won't change your URL nor save it into your browser history</a><hr />
    <a href="#void">Maybe a more readable approach, but unfortunately this one changes your URL</a>

    0 讨论(0)
  • 2021-02-01 12:54

    In my opinion, the most complete hack free solution is:

    <a href="" onclick="return false;">do absolutely nothing</a>
    
    0 讨论(0)
提交回复
热议问题