jQuery Selector How to - Compare nested selector vs adjacent to another








Question

We would like to know how to compare nested selector vs adjacent to another.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
.demo span span { /* No nested span */
  color: red;
}<!--from  www  .j a v a 2  s .c  o m-->

.demo2 span+span { /* Selects span adjacent to another span */
  color: red;
}
</style>
</head>
<body>
  <div class="demo">
    <span>1</span> <span>2</span>
  </div>
  <div class="demo2">
    <span>1</span> <span>2</span>
  </div>
</body>
</html>

The code above is rendered as follows: