jQuery: How to call function when element shows

前端 未结 7 1610
执念已碎
执念已碎 2021-01-13 11:06

I want to call a function when a div shows (after show).

Does anybody knows how could I do this? I try to use something like that:

$(#someDiv).bind(\         


        
相关标签:
7条回答
  • 2021-01-13 11:30

    jQuery live

    As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().

    0 讨论(0)
  • 2021-01-13 11:30

    I use this

    $(document).on("pagebeforeshow", "#elementID", function ()
    {
        //Whatever
    });
    
    0 讨论(0)
  • 2021-01-13 11:31
    $('#element').live('show', function(){
        // CODE
    });
    
    0 讨论(0)
  • 2021-01-13 11:32
    <script type="text/javascript" src="jquery-1.8.0.min.js"></script>
    
        <script>
    
        $(document).ready(function(){
    $("#jq_message").show(function(){
    $("#jq_message").fadeOut(3000);
    
    $("#div2").fadeOut("slow");
    
    $("#div3").fadeOut(3000);
    
    
    
        }); 
    
        });
    
        </script>
    <div id="jq_message">
    </div>   
    
    0 讨论(0)
  • 2021-01-13 11:33

    The following code (adapted from http://maximeparmentier.com/2012/11/06/bind-show-hide-events-with-jquery/) will enable you to use $('#someDiv').on('show', someFunc);.

    (function ($) {
      $.each(['show', 'hide'], function (i, ev) {
        var el = $.fn[ev];
        $.fn[ev] = function () {
          this.trigger(ev);
          el.apply(this, arguments);
          return el;
        };
      });
    })(jQuery);
    
    0 讨论(0)
  • 2021-01-13 11:34

    It must be done in the show() method, in its post-callback:

    $('#someDiv').show('slide',function(){
        alert('example')
    });
    
    0 讨论(0)
提交回复
热议问题