Definition and usage of the odd selector in jQuery

  • 2020-05-09 18:04:57
  • OfStack

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

This selector matches elements with an odd index value, counting from 0.
Grammatical structure:

$(":odd")

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

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

The above code sets the font color of li with an odd index to green in the collection of li elements.
If not used with other selectors, the default is used with * selectors. $(": odd") = $("*: odd")
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:odd").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>

I hope this article has helped you with the jQuery programming.


Related articles: