jQuery :last-child选择器
:last-child选择器语法
$('selector:last-child')
:last-child选择器语法解析
假设上面语法中的selector选择器为段落p元素,即jQuery会通过p元素选择HTML文档中所有的p元素,并将这些元素放在一个类似JS数组的集合序列中,之后:last-child选择器会进行二次匹配选择p元素的父元素中所有子元素中最后一个是p的元素,如果不是p,则不会被选择。具体的可参考下方的实例代码。
jQuery :last-child选择器实例代码
如下实例代码,段落2和段落3都是div元素中的子元素,不同的是,前者是div的最后一个子元素,而段落三不是另一个div中的最后一个子元素,因此,通过:last-child选择器进行匹配选择的时候,只会匹配选择到段落2:
<div>
<p class="p">段落1</p>
<span>span1</span>
<p class="p">段落2</p>
</div>
<div>
<p class="p">段落3</p>
<span>span2</span>
</div>
<button id="test" class="btn">jQuery</button>
<script>
$('#test').click(function(){
$('.p:last-child').css("font-size","5.5em");
})
</script>
免责声明:内容仅供参考,不保证正确性!