I use jQuery's eq () functions, but it doesn't give the correct results
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div p:eq(1)").css("border","2px solid red");
});
});
</script>
</head>
<body>
<button>Click Me!</button>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<div>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
</div>
<div>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
</div>
</body>
</html>
Expectation:
I expected 2 lines to be red (5 and 8).
Result:

Also, when I use lt (), I get the wrong results.
HTML:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div p:lt(2)").css("border","2px solid red");
});
});
</script>
</head>
<body>
<button>Click Me!</button>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<div>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
</div>
<div>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
</div>
</body>
</html>
Expectation:
I expected 4 lines to be red (6, 7, 11 and 12).
Result:
Note:
I know that as an alternative, I can use nth-child () or nth-of-type (), nth-last () or nth-last-child (), or: odd () or: even (); or: nextAll () or: prevAll (), but I want to know why this does not work !; (.
Can someone help me where the problem is?
source
share