jQuery 事件 - 元素变得可见
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10584096/
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
jQuery event - element become visible
提问by Igor Konopko
Possible Duplicate:
jQuery event to trigger action when a div is made visible
可能的重复:
当 div 可见时触发动作的 jQuery 事件
How can I run some my code when element, wich was loaded by ajax, become visible("display" is switching from "none" to "block")? Well, I need some event like
当由 ajax 加载的元素变得可见(“显示”从“无”切换到“块”)时,如何运行我的一些代码?嗯,我需要一些像
$('#element').live('show', function(){
// CODE
});
Or event that watching for deleting/adding some class to element
或监视删除/向元素添加某些类的事件
采纳答案by Igor Konopko
The problem was solved by using jquery-appear plugin
问题是通过使用 jquery-appear 插件解决的
回答by Darin Dimitrov
There's nothing built-in jQuery that allows you to achieve that. You may take a look at the livequery plugin. For example:
没有任何内置的 jQuery 可以让您实现这一目标。你可以看看livequery 插件。例如:
$('#element').livequery(function() {
// CODE
});
When an element with id="element" is added to the DOM the callback should be executed.
当一个 id="element" 的元素被添加到 DOM 时,回调应该被执行。
回答by iambriansreed
Run a check every 1 second. If the #element
exists and is visible then clear(stop) the interval and execute your code.
每 1 秒运行一次检查。如果#element
存在并且可见,则清除(停止)间隔并执行您的代码。
var checkVisible = setInterval(function(){
// if element doesn't exist or isn't visible then end
if(!$('#element').length || !$('#element').is(':visible'))
return;
// if element does exist and is visible then stop the interval and run code
clearInterval(checkVisible);
// place your code here to run when the element becomes visible
},1000);
Inevitably you have some jQuery event callback which shows the element; in those event callbacks you should place your 'when element is visible' run code.
不可避免地,您有一些显示元素的 jQuery 事件回调;在这些事件回调中,您应该放置“当元素可见时”运行代码。
回答by ubik
Have you tried jQuery.load()
?
你试过jQuery.load()
吗?
http://api.jquery.com/load-event/
http://api.jquery.com/load-event/
It should work at least for images, script tags, etc... not for elements that don't have a URL, though.
它应该至少适用于图像、脚本标签等……但不适用于没有 URL 的元素。
Otherwise, livequeryshould be of help:
否则,livequery应该会有所帮助: