Useful jQuery .eq() method
I want to mark a specific li
tag in the ul
, the HTML code is like this:
<ul>
<li>li-1</li>
<li>li-2</li>
<li>li-3</li>
<li>li-4</li>
</ul>
In order to mark it, I used .data()
method in the jQuery, just like this:
$( 'ul' ).data('show',1);
How to find the specific li tag?
I just use the JavaScript Array method:
$('li')[2].data('show',1);
but I get the error info:
undefined is not a function.
Why? when you use []
method to get the $
’s result, it’s not a jQuery object, but a JavaScript Dom object. So how to get the jQuery object? There’s two method:
Use $()
The first method is using $()
method to cover the []
result, like this
$( $('li')[2] ).data('show',1);
Use .eq()
method
The second method is using jQuery method .eq()
, you can write like this
$('li').eq(2).data('show',1);