How to make an anchor tag refer to nothing?

后端 未结 18 1481
一向
一向 2020-12-12 10:28

I use jQuery, I need to make some anchor tags perform no action.

I usually write it like this:

link

H

相关标签:
18条回答
  • 2020-12-12 10:48

    There are so many ways to do it like

    Dont add and href attribute

    <a name="here"> Test <a>
    

    You can add onclick event instead of href like

    <a name="here" onclick="YourFunction()"> Test <a>
    

    Or you can add void function like this which would be the best way

    <a href="javascript:void(0);"> 
    <a href="javascript:;">
    
    0 讨论(0)
  • 2020-12-12 10:48

    I encountered this issue on a WordPress site. The headers on dropdown menus always had the attribute href="" and the header plugin being used only allowed standard urls. The easiest solution there was just to run this code in the footer:

    jQuery('[href=""]').click(function(e){
        e.preventDefault();
    });
    

    This will prevent blank anchors from doing anything.

    0 讨论(0)
  • 2020-12-12 10:48

    React no longer support using a function like this href="javascript:void(0)" in your anchor tag, but here is something that works pretty well.

    <a href="#" onClick={() => null} >link</a>
    
    0 讨论(0)
  • 2020-12-12 10:50

    You can do it by

    <a style='cursor:pointer' >Link</a>
    
    0 讨论(0)
  • 2020-12-12 10:52

    What do you mean by nothing?

    <a href='about:blank'>blank page</a>
    

    or

    <a href='whatever' onclick='return false;'>won't navigate</a>
    
    0 讨论(0)
  • 2020-12-12 10:52

    This answer should be updated to reflect new web standards (HTML5).

    This:

    <a tabindex="0">This represents a placeholder hyperlink</a>
    

    ... is valid HTML5. The tabindex attribute makes it keyboard focusable like normal hyperlinks. You might as well use the span element for this as mentioned previously, but I find using the a element more elegant.

    See: https://w3c.github.io/html-reference/a.html
    and: https://html.spec.whatwg.org/multipage/links.html#attr-hyperlink-href

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