Questions about jquery selector.

92 3

Enter the learning jquery, have a bit of doubt about the selector, please ask your questions. Thank you.

<ul>
<li>1</li>
<li>2</li>
</ul>
<ul>
<li>3</li>
<li>4</li>
</ul>

I use the.

$('ul li:last')

It's got the last <of the second ul>, and why not two of the last for ul. What I want is 2, 4, and not just 4, which should be done.

3 Answers

112 0

Try it.

$('ul li:last').each(function () {
 console.log(this);
});

In theory, you should print two elements, that's, your writing is correct, but you understand some errors in jquery selector, which is actually an array of.

57 1

Try it.

$('ul li:last').each(function () {
 console.log(this);
});

In theory, you should print two elements, that's, your writing is correct, but you understand some errors in jquery selector, which is actually an array of.

142 0
$('ul').each(function() {
 console.log($(this).children("li:last").html());
});

( this may not be the simplest method, but read )

132 5
$('ul').each(function() {
 console.log($(this).children("li:last").html());
});

( this may not be the simplest method, but read )

136 0

That can be written.

$('ul').find("li:last")
124 2

That can be written.

$('ul').find("li:last")
...