开发者

jQuery $('.someClass').click(function(){}) only applies to elements that are currently present?

开发者 https://www.devze.com 2023-03-20 03:09 出处:网络
So I am constantly adding new points to a graph and I want all of them to have the same click function. But it seems like when I run

So I am constantly adding new points to a graph and I want all of them to have the same click function. But it seems like when I run

$('.someClass').click(function(){})

i开发者_StackOverflow社区t only applies to elements that currently have someClass. If I add a new someClass element, it does not have the click listener.

How do I get around this? Must I run click function every time I add an element?


$('.someClass').live('click', function(){});

Or even better consider delegate method.


Use jQuery live method.


Here:

$('.someClass').live('click',function(){
  // Handle here
})


The problem could be that you have not registered the new objects with the jQuery code. You can try run the javascript code above after you add the points.

If it was jsf, for example:

<h:commandButton value="Add Point>
  <f:ajax onevent="registerSomeClassObject"/>
</h:commandButton>


you can also use jquery bind method.

0

精彩评论

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