jQuery .live加载事件 [英] jquery .live on load event
问题描述
我需要一种方法来使用jquery .live()
函数对通过ajax加载的元素进行操作.
I need a way to use the jquery .live()
function to act on elements that are loaded via ajax.
例如,通过ajax .load()
For instance a div is loaded via ajax .load()
<div id="mydiv"></div>
通常我对click事件执行.live()
,但是我需要知道如何告诉dom这个新的div已加载,而没有来自用户的任何明确动作/事件.
Normally I do .live()
with a click event, but I need to know how to tell the dom that this new div has loaded without any explicit actions/events from the user.
此代码无效,但我想执行以下操作:
This code doesn't work, but I want to do something like this:
mydiv = $("#mydiv");
mydiv.live("mydiv.length > 0", function() {
// do something
});
"mydiv.length"是典型"click"或其他事件的替代物.
The "mydiv.length" being a substitue for the typical "click" or other event.
推荐答案
没有"DOM更改"事件,在浏览器上创建事件很费力.最好使用load方法的回调来触发所需的功能.
There isn't a "DOM changed" event and creating one is taxing on the browser. You are best off using the callback for the load method to trigger the functions you need to.
$('#foobar').load('myurl.html',function() {
var mydiv = $("#mydiv");
});
这篇关于jQuery .live加载事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!