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

不羁的心 提交于 2020-01-05 07:28:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!