How to make an anchor tag refer to nothing?

后端 未结 18 1479
一向
一向 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:39

    You can have an HTML anchor (a tag) without an href attribute. Leave off the href attribute & it won't link to anything:

    <a>link</a>
    
    0 讨论(0)
  • 2020-12-12 10:39

    I know this is tagged as a jQuery question, but you can answer this with AngularJS, also.

    in your element, add the ng-click directive and use the $event variable which is the click event... prevent its default behavior:

    <a href="#" ng-click="$event.preventDefault()">
    

    You can even pass the $event variable into a function:

    <a href="#" ng-click="doSomething($event)">
    

    in that function, you do whatever you want with the click event.

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

    I know this is an old question, but I thought I'd add my two cents anyway:

    It depends on what the link is going to do, but usually, I would be pointing the link at a url that could possibly be displaying/doing the same thing, for example, if you're making a little about box pop up:

    <a id="about" href="/about">About</a>
    

    Then with jQuery

    $('#about').click(function(e) {
        $('#aboutbox').show();
        e.preventDefault();
    });
    

    This way, very old browsers (or browsers with JavaScript disabled) can still navigate to a separate about page, but more importantly, Google will also pick this up and crawl the contents of the about page.

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

    Here are the three ways for <a> tag's href tag property refer to nothing:

    <a href="JavaScript:void(0)"> link </a>
    
    <a href="javascript:;">link</a >
    
    <a href="#" onclick="return false;"> Link </a>
    
    0 讨论(0)
  • 2020-12-12 10:41

    Make sure all your links that you want to stop have href="#!" (or anything you want, really), and then use this:

    jq('body').on('click.stop_link', 'a[href="#!"]',
    function(event) {
         event.preventDefault();
    });
    
    0 讨论(0)
  • 2020-12-12 10:44

    To make it do nothing at all, use this:

    <a href="javascript:void(0)"> ... </a>
    
    0 讨论(0)
提交回复
热议问题