Unable to select SPAN sibling

I can’t understand why this code changes the color of the DIV element to blue, but does not change the color of the SPAN element. Any ideas?

<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script>
    $(document).ready(function(){
      $("#prev ~ div").css("color", "blue");
      $("#prev ~ span").css("color", "red");
    });
  </script>
</head>
<body>
  <span id="prev">span#prev</span>
  <div>div sibling</div>
  <span>span sibling</span>
</body>
</html>

Noticed that if I replaced

<span id="prev">span#prev</span>

from

<p id="prev">span#prev</p>

both DIV and SPAN colors change the color of the text.

Thanks!

+3
source share
3 answers

Looks like you found a mistake.

$ ("# prev ~ span: not (#prev)") works like $ ("# prev"). siblings ("span").

+6
source

It really seems like a mistake. Report jQuery bug .

http://dev.jquery.com/report

There are a decent amount of sibling selector errors , it seems.

+4
source

, JQuery. .

+2

Source: https://habr.com/ru/post/1707191/


All Articles