Home > Web Front-end > JS Tutorial > How to select the first element in jquery

How to select the first element in jquery

coldplay.xixi
Release: 2023-01-04 09:38:04
Original
4752 people have browsed it

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)].

How to select the first element in jquery

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'});
Copy after login

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)")
Copy after login

Attached is an example of another way

<script type="text/javascript" src="/jquery-latest.js"></script>
<script>
$(function(){
 $(&#39;a&#39;).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>
Copy after login

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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template