简体   繁体   English

将事件附加到动态创建的元素

[英]Attaching event to dynamically created elements

I have some dynamically generated from a javascript plugin. 我有一些从javascript插件动态生成的。 It creates some divs with class .someclass which wraps some existing divs. 它使用类.someclass创建一些div,该div包装了一些现有的div。 I want to add some class to .someclass children. 我想为.someclass子级添加一些类。 So I put 所以我把

$(document).ready('.icheckbox',function(){
            if($(this).children().first().hasClass('checked')){
                console.log('test');
            }
        })

but the console shows nothing. 但控制台什么也没显示。 So I tried 所以我尝试了

$('body').on('.icheckbox',function(){
            if($(this).children().first().hasClass('checked')){
                console.log('test');
            }
        })

but it doesn't work either. 但它也不起作用。 Someone could help ? 有人可以帮忙吗?

Edit : here's the html code : 编辑:这是html代码:

<div class="controls">
                    <input type="checkbox" name="notif_user_like" id="notif_user_like" class="checkbox checked" value="1" /> <label for="notif_user_like" class="checkboxLabel">some text</label>
                </div>

and here's how the code turns into after the javascript was processed : 这是在处理完javascript之后代码变成的样子:

<div class="controls">
                    <div class="icheckbox" style="position: relative;"><input type="checkbox" name="notif_user_like" id="notif_user_like" class="checkbox checked" value="1" style="position: absolute; opacity: 0;"><ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; background-color: rgb(255, 255, 255); border: 0px; opacity: 0; background-position: initial initial; background-repeat: initial initial;"></ins></div> <label for="notif_user_like" class="checkboxLabel">some text</label>
                </div>

I think you want: 我想你要:

$(document).ready(function () {
    $('body').on('change', '.icheckbox', function (e) {
        if ($(this).children().first().hasClass('checked')) {
            console.log('test');
        }
    });
});

The on method is used for delegation of event handling, which is what you want for dynamically added elements as they may not exist when the handler is bound. on方法用于事件处理的委派,这是您要动态添加的元素所需要的,因为绑定处理程序时它们可能不存在。

The form is $(parentElementSelector).on(event, triggerElementSelector, handler); 形式为$(parentElementSelector).on(event, triggerElementSelector, handler);

You can read the API at http://api.jquery.com/on/ 您可以在http://api.jquery.com/on/上阅读API。

UPDATE : 更新

As pointed out by A. Wolff , you don't really need to wait for the DOM to be loaded. 正如A. Wolff指出的那样,您实际上不需要等待DOM加载。 You can simply use document as the parent element selector: 您可以简单地使用document作为父元素选择器:

$(document).on('change', '.icheckbox', function (e) {
    if ($(this).children().first().hasClass('checked')) {
        console.log('test');
    }
});

A [perhaps] better method (and one I typically implement in my own projects) is to use a closer ancestor as the parent element selector: 一种(也许)更好的方法(我通常在自己的项目中实现)是使用更接近的祖先作为父元素选择器:

$(document).ready(function () {
    $('.controls').on('change', '.icheckbox', function (e) {
        if ($(this).children().first().hasClass('checked')) {
            console.log('test');
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM