Home >Web Front-end >JS Tutorial >How to select the first element in jquery
How to select the element with jquery: Use the [eq()] method to find the element or the Nth element. The [eq()] selector selects the element with the specified index value. Code It is [$('#test').children().eq(1)].
The operating environment of this tutorial: windows7 system, jquery1.1.3.1 version, DELL G3 computer.
Recommended: jquery video tutorial
How to select the first element in jquery:
When using jquery, we often encounter that after the selector selects a group of elements, we need to find the number of elements in this group 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 select the first element in jquery. For more information, please follow other related articles on the PHP Chinese website!