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?
$(".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.
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 .
As
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.
$(".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
OR
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
Follow
answered Sep 14, năm ngoái at 13:48
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.
Required, but never shown
By clicking “Post Your Answer”, you agree lớn our terms of service, privacy policy & cookie policy
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.
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.