jQuery :nth-last-of-type(n)选择器

:nth-last-of-type(n)选择器语法

$('selector:nth-last-of-type(n)')

:nth-last-of-type(n)选择器语法解析

如果你已经阅读过上一章节:nth-of-type(n)选择器的内容,那么理解:nth-last-of-type(n)就非常简单了,因为:nth-last-of-type(n)刚好与之前介绍的选择器相反,匹配选择是selector选择器匹配选择到的元素的父元素中倒数第n个该类型的子元素。举个例子,比如,p元素选择器选择到的元素的父元素中的倒数第n个p元素(子元素)即为:nth-last-of-type(n)选择器匹配选择到的元素。

重点提示:如果是selector是一个class选择器,那么:nth-last-of-type(n)匹配选择的必须是class相同且元素类型也一致的所有元素中的倒数第n个元素!具体可参考下方的实例代码。

:nth-last-of-type(n)选择器实例代码

如下实例代码,通过类属性值为d的选择器匹配元素,然后通过:nth-last-of-type(1)的选择器选择该div中倒数第一的类属性值为d的元素,因为下方的实例中div的倒数第一的子元素的类型属性并不是d,因此不会被选择,而是选择倒数第二个和第三个的子元素,因为class为d的div和p都是各自类型元素(div和p)中元素的倒数第一:

<div>
  <div class="d">div</div>
  <p class="d">段落</p>
  <span>span</span>
</div>

<button id="nthlastoftype" class="btn">jQuery</button>

<script>
  $('#nthlastoftype').click(function(){
    $('.d:nth-last-of-type(1)').css("font-size","5.3em");
  })
</script>

免责声明:内容仅供参考,不保证正确性!


全栈前端 / jQuery教程 :






Copyright © 2022-2024 笨鸟工具 x1y1z1.com All Rights Reserved.