问题
With reference to to an previous question of mine: link
I have an additional question, as follows:
I need the ability to start the "position" at a point within the tree. Users can select a node within the tree by way of a hash (e.g. #post9) - they can click a node anywhere in the list to select it, or they can bookmark the url, which would include that node's own hash.
So my further question is: how would I locate a node within the tree and get it's position, using the hash in the URL? The hash in the URL correlates with the id of the <li>
node.
Many thanks in advance.
回答1:
Use window.location.hash
to get the element, find it and then use index()
on your list of <li>
elements to get its index/position.
var node = $(window.location.hash),
index = $('li').index(node);
来源:https://stackoverflow.com/questions/7057049/traversing-an-html-nested-list-follow-up-question-detecting-a-nodes-position