I have a menu that is generated from an XML file. The XML file is loaded using AJAX. The only html in the menu that is hard coded is:
<div id="leftNav">
<ul class="level1">
</ul>
</div>
the rest of the menu is dynamically created based on the contents of the XML file. In the Chrome inspector all the list elements that should be in the menu are there. Now when I try to count the number of list elements I get 0. I figure it's because there are no list elements hard coded on the page. The code I use to do the counting is:
alert($('.level1 > li').size());
which alerts 0.
EDIT- i misplaced the function call and the xml had not finished loading. the code does work as expected.
The function that pa开发者_如何学运维rses the XML and generates the extra list items runs before I try to count the list items.
Any ideas as to how to get the number of list items?
It's ".length" not ".size()"
alert($('.level1 > li').length);
edit — apparently I'm hilariously wrong here but I'd give ".length" a shot anyway :-) Also make sure your script isn't dropping in extraneous crap around the list of <li>
elements - try changing the selector so that it's just ".level1 li" and see if that changes anything.
You should learn about DOM because .length
or size()
are counting the li
items and there's no li
in the loaded DOM.
It has been dynamically added after the loading of the page. You should add an event handler on a button or something with the help of the .on()
function
精彩评论