[英]Observer pattern in browser-javascript: subscribe to event “Dom element added” and handle this element
I have a query: 我有一个查询:
$(function() {
$('form').each(function() {
// do smthing with form
});
});
Forms may appear dynamically, ie built by javascript. 表单可能会动态显示,即由javascript构建。 I need to handle them just after load, smth like: 我需要在加载后立即处理它们,如下所示:
$('form').onload(function() {
// it's called after new form was added to dom
});
I thought about using setInterval
, but maybe HTML5 or new ES standards brought smth new... 我考虑过使用setInterval
,但也许HTML5或新的ES标准带来了新的...
Does anybody help to compose on load callback with aforementioned characteristics? 是否有人帮助编写具有上述特征的负载回调?
PS To clarify the purpose: I need to attach event handler to event "element was added to dom". PS为了阐明目的:我需要将事件处理程序附加到事件“元素已添加到dom”中。
Wrap it in a function 将其包装在函数中
$(function() {
function modForm() {
$('form').each(function() {
// do smthing with form
});
}
modForm();
$('a').click(function(){
//add form dynamically
$('body').append('<form>');
//call the function agan
modForm();
});
});
Actually there was an event which is deprecated by now called "DOMNodeInserted", it would be wise to use delegated events . 实际上,现在有一个不推荐使用的事件称为“ DOMNodeInserted”,使用委托事件将是明智的。 with delegated events you can handle events on new elements that are dynamically add to DOM. 使用委托事件,您可以处理动态添加到DOM的新元素上的事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.