这个问题涉及使用:nth-of-type(n)哪里n是最后一个元素或的约定:last-of-type.根据定义,两个提供的n是最后一个元素没有区别,所以在理论上要么应该工作.这是一些通用的示例代码(这显然不是真正的代码,但这个简单的东西足以作为演示):
<!DOCTYPE html>
<style type="text/css">
div:first-of-type { background-color: red; }
div:last-of-type { background-color: green; } /* or div:nth-of-type(n) for last element n*/
</style>
<html>
<div>Row 1</div>
<div>Row 2</div>
</html>
Run Code Online (Sandbox Code Playgroud)
具有已知元素数量的场景中的典型约定是什么?如果只有两个元素怎么办?在这两种情况下,通常使用哪种伪选择器?
另外,一个不寻常的场景,编码器只能控制样式表和HTML,这种情况极不可能但技术上容易改变?或者这两者都属于个人偏好.