Unobtrusive Javascript Obfuscates Event Handling

前端 未结 5 1682
[愿得一人]
[愿得一人] 2021-02-09 23:08

You know what I liked best about obtrusive javascript? You always knew what it was going to do when you triggered an event.



        
5条回答
  •  既然无缘
    2021-02-09 23:37

    If you're using jQuery you can take advantage of its advanced event system and inspect the function bodies of event handlers attached:

    $('body').click(function(){ alert('test' )})
    
    var foo = $('body').data('events');
    // you can query $.data( object, 'events' ) and get an object back, then see what events are attached to it.
    
    $.each( foo.click, function(i,o) {
        alert(i) // guid of the event
        alert(o) // the function definition of the event handler
    });
    

    Or you could implement your own event system.

提交回复
热议问题