开发者

Getting AJAX content into the DOM or registering an object with jQuery via an onClick event?

I have a code setup something like below, the image is part of a block of HTML and is being loaded with AJAX so I can't get a direct handle on it using it's class or id, so I'm passing the object into a function and then picking it up with jQuery from there.

I don't like this method, is there any way of registering the image (or preferably the whole HTML block) with the DOM so I can get a direct handle on it with jQuery and be able to do something like $(".img").click(function(){}); or is there a more elegant way of doing what I'm trying to do 开发者_StackOverflow社区below:

My HTML Page:

<img src="m.jpg" onClick="vote(this);" class="img">

My JS page:

function vote(object)
{
    //alert('voted');
    $(object).css('display','none');
}

Edit: Thanks everyone who answered, +1 to all. I'm not sure who said it first, but it solved everything.


Maybe something like

$('img.img').live('click', function() {
    $(this).css('display', 'none');
});


I think the JQuery live function should sort this out for you - you could attach a click to all images of the specific class and it should reapply as the DOM is reloaded when the image is downloaded.

JQuery live function


$(document).ready(function() {
   $('.voteImg').live('click', function() {
      //alert('voted');
      $(this).hide();
   });
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜