Jquery Và Css Class, Jquery Flashcards & Practice Test

Thời trang phái mạnh và my jQuery to try showroom class="open" to the first tag:

$(".cat_lv2").click(function () $(this).parents().eq(3).addClass("open"););I don"t see anything change when I click on one of the class="cat_lv2" links.

Bạn đang xem: Jquery và css class, jquery flashcards & practice test

So, I tested it by code:

$(".cat_lv2").click(function () if( $(this).parents().eq(3).addClass("open")) alert("ok"); );and I receive a popup that says "ok".

Why won"t my code work?



You are going up too many parents. You want .eq(2), not .eq(3).

$(".cat_lv2").click(function () $(this).parents().eq(2).addClass("open"););.eq() is 0-indexed, so you need to lớn use 2 to lớn get the 3rd element.


I believe you may have misunderstood how the .parents() function in jQuery works.

The .parents() function returns the list of ancestors of the DOM object it is acting on. It looks like you were thinking that it gets the danh mục of all the DOM objects (HTML tags) which appeared before it in the HTML document.

So in your code, $(".cat_lv2") is that bunch of tags inside the tag. So their parents are that và that outer, classless tag. Neither the tag nor the tag are ancestors of that mix of tags. The is a sibling of the .

Xem thêm: Yêu Thích Phần Mềm Dự Toán Bnsc 2015, Bộ Cài Dự Toán Bnsc Phiên Bản Edu 2017 (Free)

As a result, your function should be this:

$(".cat_lv2").click(function () $(this).parents().eq(1).addClass("open"););To địa chỉ the class "open" to that outer .

RocketHazmat mentioned in a comment, your if in your demo is useless because that bit of jQuery is always returning a value which is equivalent lớn true. As per the documentation:

If an element cannot be found at the specified zero-based index, the method constructs a new jQuery object with an empty set and a length property of 0.

So .eq(3) returns an empty list, not a null, & .addClass("open") returns an object, as
RocketHazmat said, indicating that nothing was acted upon.

giới thiệu
Improve this answer
answered Sep 11, 2015 at 16:56

11366 bronze badges
add a bình luận |
If you want to change the li containing the a tag that was clicked you only need khổng lồ get the first parent:

$(".cat_lv2").click(function () $(this).parent().addClass("open"); );.open background: red; text-shadow: 0px 0px 3px white; Thời trang nam giới

But if you want lớn change the first li tag no matter which a is clicked you could traverse up to lớn the ul tag and find the first child:

$(".cat_lv2").click(function () var ul= $(this).parent().parent(); ul.find(">:first-child").addClass("open"); );.open background: red; text-shadow: 0px 0px 3px white; Thời trang nam giới


Just give the first thành quả an ID since you"re always just changing the one thing.

$(".cat_lv2").click(function () $("#firstItem").addClass("open"); );.open background: red; text-shadow: 0px 0px 3px white; Thời trang nam giới
cốt truyện
Improve this answer
answered Sep 14, năm ngoái at 13:48


4,22288 gold badges3333 silver badges4343 bronze badges
địa chỉ cửa hàng a comment |

Your Answer

Thanks for contributing an answer to lớn Stack Overflow!

Please be sure lớn answer the question. Provide details và share your research!

But avoid

Asking for help, clarification, or responding 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.

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Facebook
Sign up using email and Password

Post as a guest

email Required, but never shown

Post as a guest


Required, but never shown

Post Your Answer Discard

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

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

The Overflow Blog
Featured on Meta
What is the best way to add options khổng lồ a select from a JavaScript object with jQuery?
add table row in jQuery
How can I change an element's class with JavaScript?
jQuery get specific option tag text
How to showroom a class to a given element?
How to add days to Date?
How can I add a key/value pair to a JavaScript object?
jQuery: Get selected element tag name
How can I showroom new array elements at the beginning of an array in JavaScript?
Can't bind to lớn 'ngModel' since it isn't a known property of 'input'
Hot Network Questions more hot questions

Question feed
Subscribe to lớn RSS
Question feed to lớn subscribe lớn this RSS feed, copy và paste this URL into your RSS reader.

Stack Overflow
Stack Exchange Network
Site design / logo sản phẩm © 2022 Stack Exchange Inc; user contributions licensed under cc by-sa. Rev2022.5.18.42164

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device và disclose information in accordance with our Cookie Policy.