开发者

How select an element by class name, previously loaded in DOM

开发者 https://www.devze.com 2022-12-25 20:52 出处:网络
I write a simple piece of code that creates a div and assign it a class name: $(\'#create_div\').click(

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
  });
0

精彩评论

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