Definition and usage of the even selector in jQuery

  • 2020-05-09 18:05:15
  • OfStack

This article illustrates the definition and use of the even selector in jQuery. Share with you for your reference. The specific analysis is as follows:

This selector matches all elements with an even index value, counting from 0.

Grammatical structure:

$(":even")

This selector 1 should also be used in conjunction with other selectors, such as class selectors, element selectors, and so on.
Such as:

$("li:even)").css("color","green")

The above code sets the font color of li with an even index to green in the collection of li elements.
If not used with other selectors, the default is used with * selectors. $(":even") = $("*:even")
Example code:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="//www.ofstack.com/" />
<title> The home of the script </title>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("#btn").click(function(){
    $("li:even").css("color","blue");
  });
});
</script>
</head>
<body>
<ul>
  <li>html The zone </li>
  <li>div+css The zone </li>
  <li>jQuery The zone </li>
  <li>Javascript The zone </li>
</ul>
<button id="btn"> Click to view </button>
</body>
</html>

The above code can set the font color to blue in the li element with an index value of even.

I hope this article has been helpful to your jQuery programming.


Related articles: