Jquery on curl fetched data
I have a div which is being populated thro开发者_StackOverflow社区ugh curl from another html page. Now the data is populating correctly but I want to use jQuery or Javascript on the ids or classes of the fetched data. I tried using livequery, binding but of no use. Can you please help me?
Are you wrapping your javascript with $(document).ready(function() { } ); as to ensure the dom is fully loaded?
For instance:
$(document).ready(function() { $('#id').click(function() { // do stuff } ); }
This would fix the issue if the HTML fetched via CURL is being populated via a direct post-back. If you're populating the data asynchronously, then you'll either have to use the .live() mechanism for binding (through read the docs carefully, live() doesn't work in certain scenarios and was added in the most recent version of jQuery -- so check that you've got the right version of jQuery and that you're not trying to use events that aren't supported with that binding mechanism). Furthermore, live is a bit of performance tax, so I'd do something like.
$.ajax({ // Lots of ommitted options for simplicity's sake
success: function(data, status) {
$('#targetDiv').append(data);
$('#targetDiv #elementID').click(function() { // do stuff });
}
});
Hopefully this helps steer you in the right direction. With a code sample we could certainly help more.
Basically you're trying to bind to elements that do not yet exist. JavaScript works directly on the DOM (Document Object Model). What that means, is that whenever a specific element does not yet exist in the DOM, JavaScript can't bind to it.
Using jQuery does not change this, or (at best) only does so under very specific circumstances. I would just write a wrapper function in which you bind to the new elements, that you call after the AJAX request was completed successfully.
精彩评论