开发者

Traversing an html nested list, follow-up question - detecting a node's position

开发者 https://www.devze.com 2023-03-28 13:31 出处:网络
With reference to to an previous question of mine: link I have an additional question, as follows: I need the ability to start the \"po开发者_C百科sition\" at a point within the tree. Users can sele

With reference to to an previous question of mine: link

I have an additional question, as follows:

I need the ability to start the "po开发者_C百科sition" 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.


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号