javascript 任何方法,如用于 div 的 onshow()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6373263/
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-10-25 20:26:06  来源:igfitidea点击:

any method like onshow() for a div

javascriptjqueryasp.netjquery-ui

提问by user695663

I have a div that is shown or hidden dynamically. I want some function to execute when the div is shown for the first time. Let me know how to do that.

我有一个动态显示或隐藏的 div。我想要一些函数在第一次显示 div 时执行。让我知道怎么做。

<div id="firstDiv"></div>
<div id="secondDiv"></div>

so when $('#secondDiv").show();I want to perform some function.

所以当$('#secondDiv").show();我想执行某些功能时。

How I can do that?

我怎么能做到这一点?

回答by James Allardice

You can provide a callback function to the showfunction, which will be executed upon completion of show:

您可以为该show函数提供一个回调函数,该函数将在完成时执行show

$("#secondDiv").show(function() { 
    //Do something 
});

As mentioned in the comments, this will behave as an animation. If you want showto behave as normal (i.e. the element appears instantly with no animation) you need to supply a duration parameter of 0 to the showfunction. The difference is demonstrated in this fiddle.

正如评论中提到的,这将表现为动画。如果您想show表现得正常(即元素立即出现而没有动画),您需要为show函数提供持续时间参数 0 。差异在这个 fiddle 中得到了证明。