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
any method like onshow() for a div
提问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 show
function, 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 show
to behave as normal (i.e. the element appears instantly with no animation) you need to supply a duration parameter of 0 to the show
function. The difference is demonstrated in this fiddle.
正如评论中提到的,这将表现为动画。如果您想show
表现得正常(即元素立即出现而没有动画),您需要为show
函数提供持续时间参数 0 。差异在这个 fiddle 中得到了证明。