HTML anchor link - href and onclick both?

后端 未结 4 2025
耶瑟儿~
耶瑟儿~ 2020-11-28 21:38

I want to author an anchor tag that executes some JavaScript and then proceeds to go wherever the href was taking it. Invoking a function that executes my JavaS

4条回答
  •  离开以前
    2020-11-28 22:07

    When doing a clean HTML Structure, you can use this.

    //Jquery Code
    $('a#link_1').click(function(e){
      e . preventDefault () ;
      var a = e . target ;
      window . open ( '_top' , a . getAttribute ('href') ) ;
    });
    
    //Normal Code
    element = document . getElementById ( 'link_1' ) ;
    element . onClick = function (e) {
      e . preventDefault () ;
      
      window . open ( '_top' , element . getAttribute ('href') ) ;
    } ;
    Do it!

提交回复
热议问题