Home >Web Front-end >JS Tutorial >jQuery: eq() selector usage example_jquery

jQuery: eq() selector usage example_jquery

WBOY
WBOYOriginal
2016-05-16 16:23:341019browse

The example in this article describes the usage of:eq() selector in jQuery. Share it with everyone for your reference. The specific analysis is as follows:

This selector matches an element with a given index value.
The position index of the
element starts from 0.

Grammar structure:

Copy code The code is as follows:
$(":eq(index)")

This selector is generally used in conjunction with other selectors, such as class selectors, element selectors, etc. For example:

Copy code The code is as follows:
$("li:eq(1)").css("color", "green")

The above code sets the font color in the li with index 1 (that is, the second li) in the li element collection to green.

If not used with other selectors, the default state is to be used with the * selector, for example, $(":eq()") is equivalent to $("*:eq()").

Parameter list:

参数 描述
index 给定的索引值。

Example code:

Example 1:

Copy code The code is as follows:






:eq selector-Script Home





  • html area

  • div css area

  • jQuery Zone

  • Javascript Zone





The above code can set the font color in the li element with an index value of 1 in the li element collection to green. Because the index value starts from 0, the above code sets the font color in the second li is green.

Example 2:

Copy code The code is as follows:






:eq selector-Script Home





  • html area

  • div css area

  • jQuery Zone

  • Javascript Zone



Script House welcomes you


The sun is out




Since the above code does not specify a selector to be used with the :eq() selector, it is used with the * selector by default, which can match all elements, so that the element with an index value of 10 will be used. The font color is set to green.

I hope this article will be helpful to everyone’s jQuery programming.

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