[英]Automatically call jQuery functions on new dom elements
Is it possible to call an jQuery function on newly matched items automatically? 是否可以自动在新匹配的项目上调用jQuery函数?
For example I have the following code: 例如,我有以下代码:
$(document).ready(function(){
$('[draggable]').draggable();
});
This adds the 'draggable' to each element which matches [draggable] however when further along the road new elements with the attribute 'draggable' are added those are not getting the 'draggable()' function getting called on them. 这会将“draggable”添加到与[draggable]相匹配的每个元素中,但是当进一步沿着道路添加具有属性“draggable”的新元素时,那些没有得到'draggable()'函数被调用它们。
Is it possible to monitor the DOM or something and also call this method on each new dom item which matches the selector? 是否可以监视DOM或其他东西,并在每个匹配选择器的新dom项目上调用此方法?
I know there is something like this for 'click' events and such (the jquery delegate method) but as far as I know I can't seem to use that for this case. 我知道对于'click'事件有这样的事情(jquery委托方法),但据我所知,我似乎无法在这种情况下使用它。
Check " Mutation Events " there is an event called DOMNodeInserted maybe it helps you 检查“ Mutation Events ”有一个名为DOMNodeInserted的事件,它可能对你有帮助
by the way, check: JQuery - use element with DOMNodeInserted 顺便说一下,检查: JQuery - 使用带有DOMNodeInserted的元素
You can use the arrive.js library that I developed for the exact same purpose (it uses MutationObserver internally). 您可以使用我为完全相同的目的开发的arri.js库(它在内部使用MutationObserver )。 Usage: 用法:
document.arrive('[draggable]', function(){
// 'this' refers to the newly created element
$(this).draggable();
});
there was ".live()" for jQuery, but i see it's deprecated?! 有 “.live()” jQuery的,但我看到它的过时? don't get the transformation from ".live()" to the new ".on()"-method currently, but take a look @ yourself and maybe ask in their forum... this should be the right way to do... 不要从“.live()”转换到新的“.on()” - 当前的方法,但看看@自己,也许在他们的论坛中问...这应该是正确的方法。 ..
.on()
is what you need if you are running jQuery 1.7 or later. 如果您运行的是jQuery 1.7或更高版本,则需要.on()
。 It will run on elements as they are added to the page, as well as those already in place when it's called. 它将在元素添加到页面时运行,以及在调用时已经存在的元素。 If you're using an earlier version, take a look at the .live()
method, which has since been deprecated but has the same functionality with added elements. 如果您使用的是早期版本,请查看.live()
方法,该方法已被弃用但与添加的元素具有相同的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.