javascript - css中类的第二个实例的快捷方式

标签 javascript html css

我有一点问题,我必须链接到我无法控制的网页中的特定部分。

这个页面没有很多漂亮、独特的元素,所以我可以只使用 www.thepage.com#yayforcss,而是有相同元素的多个实例。

有什么方法可以链接到网页中某个类的第 x 个实例吗?

最佳答案

没有办法(至少不使用一些大量的 JS 代码)在多个元素上使用相同的 ID 并使用带有标签的 URL 指向正确的元素。

W3.org html and css specifications about IDs

  1. 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

  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/

相关文章:

javascript - 如何阻止用户使用浏览器的后退按钮或退格键访问上一个jsp页面

javascript - Angular 复选框过滤器不留下任何数据

javascript - Sketch- Pad 代码不工作

javascript - 为什么 bootstrap 3 在我的按钮和其他 html 元素中设置蓝色矩形边框?

javascript - 如何在另一个模态中创建模态,最后一个模态在 Jquery 中打开另一个时消失

javascript - 使用 jQuery 检查 radio 时,检查 radio 是否被选中不起作用

javascript - 叠加的 HTML Canvas 元素

javascript - 如何删除具有云功能的 Node ?

html - 如何将页脚放在页尾?

javascript - 使用 jQuery 递归更改元素类型,为什么它部分起作用?