CSS3 nth-of-type across the entire page?

南笙酒味 提交于 2019-12-08 21:50:49

问题


It seems to me that nth-of-type only works within the same parent element. Is there any way to get it work across the whole page?

My situation: I would like to cycle through five hover colors for links. These links are scattered across many paragraphs. Since there are only one or two links per paragraph, the first couple of hover colors are disproportionately favored.

Thanks!


回答1:


nth-of-type always looks at the element's index relative to it's direct parent: (w3schools), so it won't work across the whole page.

Your best bet is to implement this behaviour with javascript, here's a quick demo using JQuery: jsfiddle

var styles = ["first", "second", "third"];
var index = 0;
$("body").find("a").each(function() {
    $(this).addClass(styles[index++]);
    if (index >= styles.length) index = 0;
});


来源:https://stackoverflow.com/questions/17159644/css3-nth-of-type-across-the-entire-page

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