Jquery method to get the number of elements: Use the [eq()] method to find the number of elements or the Nth element, the code is [$('#test').children().eq( 1).css({'display':'inline-block'})].
jquery's method of getting the first element:
Often encountered when using jquery, selector selection After a set of elements, you need to find the number of elements in this set of elements.
Use the eq()
method in jquery to find the first element or the Nth element. The usage of eq()
in jquery is as follows:
eq()
The selector selects elements with the specified index value.
index values start from 0, and the index value of all first elements is 0 (not 1).
is often used with other elements/selectors to select elements with a specific sequence number in a specified group.
Example:
$('#test').children().eq(1).css({'display':'inline-block'});
Set the style of the second child element of the element with id test to 'display':'inline-block'
.
Another way of writing
$(":eq(index)") 如:$("p:eq(1)")
Attached is an example of another way
<script type="text/javascript" src="/jquery-latest.js"></script> <script> $(function(){ $('a').each(function(i){ this.onclick=function(){ alert(i); return false; }; }); }); </script> <a href="">百度</a> <a href="">google</a> <a href="http://www.111cn.net">msn</a> <a href="">qq</a>
Or write like this
<script type="text/javascript" src="jquery-1.1.3.1.js"></script> <script type="text/javascript"> $(function() { $("a").bind("click",function() { alert($("a").index(this)); } ) } ) </script>
Related free learning recommendations: JavaScript (video)
The above is the detailed content of How to get the first element in jquery. For more information, please follow other related articles on the PHP Chinese website!