I've loaded file in div
$("#div").load("file.txt");
That file has html images .
I want to access them by $('#div>img') (or other selectors), but nothing happens. If they were in page initially, there are no problems.
How can you access loaded content by jQuery as part of the usual page?
-
can you provide a sample of what's inside file.txt?Darryl Hein– Darryl Hein2010年03月03日 08:30:58 +00:00Commented Mar 3, 2010 at 8:30
-
1Are you trying to access them immediately, or are you actually waiting for the load to complete (callback)?deceze– deceze ♦2010年03月03日 08:34:07 +00:00Commented Mar 3, 2010 at 8:34
-
2Are you sure you accessing them after file has been loaded and HTML got inserted into DOM?vava– vava2010年03月03日 08:34:59 +00:00Commented Mar 3, 2010 at 8:34
-
<img src='images/jp.gif' /> .live solved the problem. it was after ful loadingQiao– Qiao2010年03月03日 08:57:30 +00:00Commented Mar 3, 2010 at 8:57
3 Answers 3
See jquery doesn't see new elements for one possible problem you might be having. Use the change method to wait until they enter the DOM. e.g.
$("#div").load("file.txt");
$('#div>img').change(function(){
var images = $('#div>img');
// Do something...
}
Comments
The content loaded (supposedly through Ajax) is rendered as a text string. It's up to you to load it into the DOM using .innerHTML and such. In jQuery you can use .html() to load the content inside the DOM and then you're going to be able to access it through standard jQuery DOM selectors.
2 Comments
$("#div").load("file.txt");
$("#div > img")... // file.txt hasn't finished loading yet
Will probably not work, since loading the file happens asynchronously and takes some time, and the content is simply not there yet when you try to access it. For this reason, you can specify a callback that is guaranteed to run after the load() has finished:
$("#div").load("file.txt", function () {
// file.txt has finished loading, this should work:
$("#div > img")...
});