How select an element by class name, previously loaded in DOM
I write a simple piece of code that creates a div and assign it a class name:
$('#create_div').click(
function() {
div = $("<div>").addClass("myClass");
$("body").append(div);
}
);
Ok: after "create_div" button is fired the function appends the new div to body container.
Now... : How to select the new element created ? How do I reach it? I have tried:
$('.myClass').click(
function() {
// do some开发者_JAVA技巧thing
}
);
but it doesn't works. Thanks for the help!
$('.myClass').live('click',
function() {
// do something
}
);
Should bind to an element that is created after the DOM is loaded.
This would also work:
div = $("div").addClass("myClass"); //NOTE: not <div> !!
$("body").append(div);
div.click(function(e) {
//Do Something
});
精彩评论