Home  >  Article  >  Web Front-end  >  How to get the first element in jquery

How to get the first element in jquery

coldplay.xixi
coldplay.xixiOriginal
2020-11-18 14:45:561968browse

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

How to get the first element in jquery

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



百度
google
msn
qq

Or write like this


    

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!

Statement:
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