定位页面上嵌套(?)锚标记的内容

Locating content on page with nested(?) anchor tags?

本文关键字:嵌套 定位      更新时间:2023-09-26

Asume,我的网站是domain.com,有一个商店页面,domain.com/store通过将id = "itemid"添加到每个项目中,我可以让用户直接访问该项目:

domain.com/store#itemid

通常的锚点行为。但是,我需要一些更复杂的东西。

假设我的条目层次结构如下:

<div id="item1>
   <div class="images"></div>
</div>

我希望用户通过使用类似锚的寻址直接到达给定项目的"images"框。如

domain.com/store#itemid.images

最好的方法是什么?

这样的东西应该工作,分割哈希分为两部分:id和类,然后使用然后使用javascript或jquery找到该元素,并滚动到视图

hash = window.location.hash.split('.')
document.getElementById(hash[0]).getElementsByClassName(hash[1])[0].scrollIntoView(true);

为什么不尝试用项目id来命名子div呢?

比如

<div id="item1>
   <div class="item1_images"></div>
   <div class="item1_ex"></div>
</div>

那么你可以使用domain.com/store#itemid1_images