English 中文(简体)
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 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();
   });
});




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签