我有一点问题,我必须链接到我无法控制的网页中的特定部分。
这个页面没有很多漂亮、独特的元素,所以我可以只使用 www.thepage.com#yayforcss,而是有相同元素的多个实例。
有什么方法可以链接到网页中某个类的第 x 个实例吗?
最佳答案
没有办法(至少不使用一些大量的 JS 代码)在多个元素上使用相同的 ID 并使用带有标签的 URL 指向正确的元素。
W3.org html and css specifications about IDs
The id attribute assigns a unique identifier to an element (which may be verified by an SGML parser). - http://www.w3.org/TR/html401/struct/global.html#h-7.5.2
What makes attributes of type ID special is that no two such attributes can have the same value; whatever the document language, an ID attribute can be used to uniquely identify its element. - http://www.w3.org/TR/CSS2/selector.html#id-selectors
如果您想直接链接到内容的特定部分 - 为每个部分使用唯一的 ID:
<div id="section_1">some content</div>
<div id="section_2">some content</div>
<div id="section_3">some content</div>
<div id="section_4">some content</div>
然后访问这些部分:
www.example.com/page/#section_1
www.example.com/page/#section_2
www.example.com/page/#section_3
www.example.com/page/#section_4
如果您有适用于当前 ID 的 CSS 样式 - 您必须更改它并使用类。
关于javascript - css中类的第二个实例的快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26550654/