开发者

why does calling methods on a div fire the live click event

i h开发者_开发百科ave this jquery code:

 $(this).parent('.copyFoodInstance').attr("myset", "0");
 $(this).parent('.copyFoodInstance').html("<img  BORDER=0 src='../../images/copy1.png' />");

it seems that when i call this code, this event fires:

$(document).ready(function() {
    $('div.copyFoodInstance').live('click', function() {

does anyone know why this event fires from the lines above? is there anyway to stop if from firing? All i am doing is updating an attribute on the div and updating the html. this shouldn't fire a click event?


That code should not fire the click event, you have something else going on there or invalid HTML messing with things (invalid HTML has all sorts of crazy side-effects with random behavior sometimes).

I would first check that your HTML is valid by going here: http://validator.w3.org/ Then check the following:

  • Are you clicking something inside that div, and the event happens because of a bubble?
  • Are you absolutely sure no other event handlers are attached to that element?
  • Do you have multiple nested .copyFoodInstance? If so you may want .closest() instead.
  • Instead of .attr() setting an invalid attribute, I would use .data() for storage.
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜