Returning false on link click jquery

后端 未结 4 1551
孤街浪徒
孤街浪徒 2021-01-24 22:33

I wish to have a link which opens popup when clicked however I wish it to open a page in a new window if the user doesn\'t have JS enabled.

The following doesn\'t seem t

相关标签:
4条回答
  • 2021-01-24 22:55

    You simply missed the # indicating it's an id and not a node name.

    $('#tac-link')
    


    PS - I wouldn't recommend using eval, why not just store all the page variables in an object?

    var pages = {};
    
    function popUp( url ) {
        var id = +new Date;
        pages[id] = window.open( url );
    }
    
    0 讨论(0)
  • 2021-01-24 22:57

    In order to make this functionality more browser-compatible, you need to pass the event object to the click handler, then call e.preventDefault();.

    example:

    $( '#button' ).click( function( e ) {
        //... your code...
    
        e.preventDefault();
        return false;
    } );
    
    0 讨论(0)
  • 2021-01-24 23:04

    I think you just have the id selector wrong. Try changing $('tac-link') to $('#tac-link').

    Another approach:

    $('a[target="_blank"]').click(function(e) {
        window.open( this.href );
        e.preventDefault();
    });
    
    0 讨论(0)
  • 2021-01-24 23:08

    Also note that event.preventDefault is not present in all browsers and will cause an error in things like IE7 (if memory serves.) To avoid that, use a general if-statement:

    if (e.preventDefault) {  
        e.preventDefault();  
    }  
    e.returnValue = false;
    

    This will prevent the error from being thrown. I believe jQuery uses this approach as well.

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