Possible to select (inner)text of content that is loaded through a async (ajax) call?
I have a simple tree view that is loading child nodes through a ajah call to the server. I'm going to abbreviate the html but you should get the gist.
<li id=1>Node 1开发者_运维百科</li>
When this is expanded (by being clicked on) there will be a bunch of sub nodes loaded through a ajah call (they are not on the page to begin with):
<ul>
<li id=1_1>Node 1_1</li>
<li id=1_2>Node 1_2</li>
<li id=1_3>Node 1_3</li>
</ul>
and then again for Node 1_1
<ul>
<li id=1_1_1>Node 1_1_1</li>
<li id=1_1_2>Node 1_1_2</li>
<li id=1_1_3>Node 1_1_3</li>
</ul>
Now that we got all that on the screen I want to have something like so (this is simplified to try and remain clear). I am using jQuery:
$('li').live('click', function() {
var path = $('li').attr('id');
var parent = '#1_1'; // this would be calculated, assuming 1_1_ node was clicked
var grandParent = '#1'; // against calculated
var crap = $(parent).text(); // should be 'Node 1_1'
var darn = $(grandParent).text(); // should be 'Node 1'
});
Both crap and darn are not getting any values. I think this is because they aren't on the page and need a something like a "live" selector, similar to jquery's "live" events.
I think this answer on this question might be what I want, but not sure if it is the most efficient...so I posted a new question. I will try doing this and see what I come up with and post results. jQuery Ajax - Get Elements Inner Text
The issue though is I will have potentially 10,000 nodes visible on the screen at once so reloading and then filtering just seems slower than a direct getById type of selection.
I just tried the code as plain Html it works perfectly. As you mentioned if some of the "li" elements are not on the page, it will not fire for them.
So using jQuery's "Live query" Plugin you will not need to use the jQuery Click event handler again and again, also no need to filter or reload either.
http://plugins.jquery.com/project/livequery/
精彩评论