Make a HTML link that does nothing (literally nothing)

前端 未结 9 553
耶瑟儿~
耶瑟儿~ 2020-12-13 23:35

I want a link that does nothing. I don\'t want this:

because then the URL becomes something.com/whatever/#.

相关标签:
9条回答
  • 2020-12-13 23:53

    <a href="javascript:;">Link text</a> - that's what I usually use

    0 讨论(0)
  • 2020-12-13 23:53

    DONT USE <a>... instead use <span class='style-like-link'> and then use the class to style it however you want.

    0 讨论(0)
  • 2020-12-13 23:58

    We can achieve that using javascript void which normally involves evaluation of an expression and returning undefined, which includes adding javascript:void(0); on the href.

    The void operator is usually used merely to obtain an undefined primitive value, usually using “void(0)” (which is equivalent to “void 0”). In these cases, the global variable undefined can be used instead (assuming it has not been assigned to a non-default value).

    a {
      text-decoration: initial;
    }
    <a href="javascript:void(0);"> This link actually does nothing when clicked</a>

    0 讨论(0)
  • 2020-12-13 23:59

    one way which no one has mentioned is to point the href to an empty local file location like so

    <a href='\\'>my dead link</a>
    

    why? If you use a framework such as react or angular, the compiler will spit out some warnings which can make your log or console dirty. This technique will also prevent robots or spiders from incorrectly linking things.

    0 讨论(0)
  • 2020-12-14 00:08

    The following will prevent your href from being ran

    <a href="#" onclick="return false;">
    

    If you are using jQuery, event.preventDefault() can be used

    0 讨论(0)
  • 2020-12-14 00:08

    Don't make it a link (although it is prefered to do it) and style it with CSS so that it looks like a link:

    p.not-a-link { text-decoration: underline; cursor: pointer } 
    

    Or even better just make it a link and let the javascript function which is used e.preventDefault() to prevent the link.

    Also add the link to the href so that users without JS enabled will still be able to use it (as a fallback).

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