jQuery:如何在元素显示时调用函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15232688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:38:42  来源:igfitidea点击:

jQuery: How to call function when element shows

jquerybindshow

提问by user1364684

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

我想在 div 显示时(显示后)调用一个函数。

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

有谁知道我怎么能做到这一点?我尝试使用类似的东西:

$(#someDiv).bind('show',function(){
    alert('example')
});

But I don't sure if I do that from correct way or if it is possible or not achieve that. Any ideas?

但我不确定我是否以正确的方式这样做,或者是否可能或无法实现。有任何想法吗?

采纳答案by aloplop85

jQuery live

jQuery 直播

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().

从 jQuery 1.7 开始,不推荐使用 .live() 方法。使用 .on() 附加事件处理程序。旧版本 jQuery 的用户应该优先使用 .delegate() 而不是 .live()。

回答by JellicleCat

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);.

以下代码(改编自http://maximeparmentier.com/2012/11/06/bind-show-hide-events-with-jquery/)将使您能够使用$('#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);

回答by Nelson

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

它必须在 show() 方法的后回调中完成:

$('#someDiv').show('slide',function(){
    alert('example')
});

回答by Aman Attari

<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>   

回答by MarceloBarbosa

I use this

我用这个

$(document).on("pagebeforeshow", "#elementID", function ()
{
    //Whatever
});

回答by Jon Harding

$('#element').live('show', function(){
    // CODE
});

回答by PSR

try this

尝试这个

$(#someDiv).bind('show',function(){
    callFunction();
});

or

或者

$(#someDiv).on('show',function(){
    callFunction();
});

function callFunction() { ............ }

函数 callFunction() { ............ }