开发者

Is there a JQuery event to determine when an element's list of children has changed?

开发者 https://www.devze.com 2023-02-11 07:18 出处:网络
I\'m hoping to find a JQuery event that will tell me when an element collection has grown or shrunk af开发者_运维知识库ter the addition/removal of a child element.What you are looking for are the DOMN

I'm hoping to find a JQuery event that will tell me when an element collection has grown or shrunk af开发者_运维知识库ter the addition/removal of a child element.


What you are looking for are the DOMNodeInserted and DOMNodeRemoved events.

For example, after you bind to a DOMNodeInserted event of a list:

$('ol').bind('DOMNodeInserted', function() {
    alert('node inserted');
});

and then another list item is inserted:

$('ol').append('<li>x</li>');

the event is fired.


You can count child elements initially then use a conditional statement to check whether count changed.

<div>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
</div>

alert($('div').children().length);
0

精彩评论

暂无评论...
验证码 换一张
取 消