Attempting to trigger before & after events around jQuery show()

后端 未结 3 650
悲哀的现实
悲哀的现实 2021-02-15 02:02

I\'m trying to get a piece of our JavaScript run after some 3rd party code makes a modal dialog appear. I saw a pretty neat idea of hijacking the jQuery show function, but unfor

3条回答
  •  南笙
    南笙 (楼主)
    2021-02-15 02:46

    I found another solution to this over here, with a slight tweak of my own.

    (function($){
        $.each(['show','hide'],function(i,ev){
            var el = $.fn[ev];              
            $.fn[ev] = function(){
                this.trigger(ev);
                return el.apply(this,arguments);
            };
        });                  
    })(jQuery);
    

    It doesn't offer the before/after events of other examples but it could certainly be elaborated on. It's working for me so far and hope it continues to do so--it's a nice little approach that can easily be expanded to other jQuery functions, including the various alternatives to show.

    ** Note this also fires an event for "hide".

提交回复
热议问题