当前位置:网站首页>How JQ gets the reciprocal elements

How JQ gets the reciprocal elements

2022-06-27 05:03:00 Front cut zz

<ul>
	<li> Penultimate </li>
	<li> The third from the bottom </li>
	<li> second to last </li>
	<li> The last one </li>
</ul>

jq Get last element :

var text = $('ul li').last().text()
console.log(text)

Print the results :
 Insert picture description here

jq Get the penultimate element :

var text = $('ul li').eq(-2).text()
console.log(text)

Print the results :
 Insert picture description here

expand :

eq() Method : Returns the element with the specified index number of the selected element .

grammar

$(selector).eq(index)

index There has to be , Index used to specify elements . It can be an integer or a negative number . among , Using a negative number will calculate the index from the end of the selected element .

原网站

版权声明
本文为[Front cut zz]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270458342256.html