遍历html嵌套列表,后续问题 - 检测节点的位置 [英] Traversing an html nested list, follow-up question - detecting a node's position

查看:167
本文介绍了遍历html嵌套列表,后续问题 - 检测节点的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于我之前的一个问题:
链接



我还有一个问题,如下所示:



我需要启动位置在树的一个点。用户可以通过散列(例如#post9)来选择树中的一个节点 - 他们可以单击列表中任何位置的节点来选择它,或者它们可以为包含该节点自己的散列的URL添加书签。



所以我的另一个问题是:如何在树中找到一个节点并获取它的位置,使用URL中的哈希? URL中的哈希值与< li> 节点的ID相关。



提前非常感谢。

解决方案

使用 window.location.hash 获取元素,找到然后在您的< li> 元素列表中使用 index()来获取其索引/位置。 / p>

  var node = $(window.location.hash),
index = $('li')。index节点);


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.

解决方案

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

这篇关于遍历html嵌套列表,后续问题 - 检测节点的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆