call javascript function on hyperlink click

前端 未结 9 1966
陌清茗
陌清茗 2020-11-30 21:13

I am dynamically creating a hyperlink in the c# code behind file of ASP.NET. I need to call a JavaScript function on client click. how do i accomplish this?

9条回答
  •  广开言路
    2020-11-30 21:50

    Neater still, instead of the typical href="#" or href="javascript:void" or href="whatever", I think this makes much more sense:

    var el = document.getElementById('foo');
    el.onclick = showFoo;
    
    
    function showFoo() {
      alert('I am foo!');
      return false;
    }
    
    Show me some foo
    

    If Javascript fails, there is some feedback. Furthermore, erratic behavior (page jumping in the case of href="#", visiting the same page in the case of href="") is eliminated.

提交回复
热议问题