jquery委托onload事件处理程序

jquery委托onload事件处理程序

本文介绍了jquery委托onload事件处理程序,用于新创建的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将事件处理程序委托给尚未创建的元素:

I am delegating event handler to elements that are not yet created like this:

$('body').on('change', '#some_elementid', function() {
  //actions
});

这是有效的,

但是我希望 actions 在创建元素后立即运行而不需要任何其他事件。 load 似乎无法正常工作..

But I want actions to run right after the element is created without any other events. load doesnot seem to work..

这里有什么可以实现 onload

what is possible here to achieve onload ?

更新

<div id="some_elementid">
    <button id="addmore">+ add more</button>
</div>

点击 +添加更多,另一个容器将插入 #some_elementid ,其中包含一些字段。我想在插入期间预先填充这些字段。

by clicking on + add more, another container will be inserted into #some_elementid which will have some fields. I want to prepopulate those fields during inserting.

没有预先填充,它看起来像:

without prepopulation, it will look like:

<div id="some_elementid">
    <div>
      <input type="text" id="id_0">
      <input type="text" id="id_1">
      ...
    </div>
    <button id="addmore">+ add more</button>
</div>

我想在onload期间进行操作并显示如下:

I want to manipulate during onload and show like:

 <div id="some_elementid">
    <div>
      <input type="text" id="id_0">
    </div>
    <button id="addmore">+ add more</button>
</div>

JSfiddle

我想要隐藏插入时有两个。因为稍后点击其他一些单选按钮,两个将显示,但现在不显示。这就是我想要实现的目标

I want to hide "two" while inserting. because later on clicking some other radio button, "two" will show up, but not now. this is what I am trying to achieve

推荐答案

您可以在支持它的所有浏览器上捕获事件:

You could capture event on all browsers which support it:

document.addEventListener(
    'load',
    function(event){
        var elm = event.target;
        if( elm.id === 'some_elementid'){ // or any other filtering condition
            // do some stuff
        }
    },
    true // Capture event
);

这篇关于jquery委托onload事件处理程序,用于新创建的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:09