I have an unordered các mục & the index of an li tag in that danh mục. I have sầu to get the li element by using that index và change its background color. Is this possible without looping the entire list? I mean, is there any method that could achieve sầu this functionality?

Here is my code, which I believe sầu would work...

Bạn đang xem:

India Indonesia Trung Quốc United States United Kingdom



$(...) // gives you the DOM element at index$(...).get(index) // gives you the DOM element at index$(...).eq(index) // gives you the jQuery object of element at indexDOM objects don"t have sầu css function, use the last...

$("ul li").eq(index).css("background-color":"#343434");docs:

.get(index) Returns: Element

Description: Retrieve sầu the DOM elements matched by the jQuery object.

.eq(index) Returns: jQuery

Description: Reduce the phối of matched elements to lớn the one at the specified index.


You can use jQuery"s .eq() method to get the element with a certain index.

Xem thêm: Phần Mềm Whatsapp - Top 5 Lựa Chọn Thay Thế Whatsapp Năm 2021

$("ul li").eq(index).css("background-color":"#343434");


You can use the eq method or selector:

There is another way of getting an element by index in jQuery using CSS :nth-of-type pseudo-class:

There are other selectors that you may use with jQuery to match any element that you need.

You could skip the jquery và just use CSS style tagging:

India Indonesia Đài Loan Trung Quốc United States United Kingdom
Thanks for contributing an answer to lớn Staông chồng Overflow!

Please be sure lớn answer the question. Provide details và cốt truyện your research!

But avoid

Asking for help, clarification, or responding to lớn other answers.Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Post Your Answer Disthẻ

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy & cookie policy

Not the answer you're looking for? Browse other questions tagged jquery dom get or ask your own question.

Is there a general way lớn index in jQuery? I know there's .first() but how about .second() or .third()
site kiến thiết / biểu tượng logo © 2021 Staông xã Exchange Inc; user contributions licensed under cc by-sa. rev2021.10.4.40368

Your privacy

By clicking “Accept all cookies”, you agree Staông chồng Exchange can store cookies on your device & disclose information in accordance with our Cookie Policy.