logo

  • Hỏi Đáp
  • Kiến Thức
  • Sức Khỏe
  • Tử Vi
  • Công Nghệ
No Result
View All Result
logo
No Result
View All Result
Trang chủ jquery : get index of element as child relative to parent

JQUERY : GET INDEX OF ELEMENT AS CHILD RELATIVE TO PARENT

by Admin _ May 07, 2022

$("#wizard li").click(function () // alert index of li relative khổng lồ ul parent);How can I get the index of the child li relative to lớn it"s parent, when clicking that li?

For example, when you click "Step 1", an alert with "0" should pop up.

Bạn đang xem: Jquery : get index of element as child relative to parent


*

*

$("#wizard li").click(function () console.log( $(this).index() ););However rather than attaching one click handler for each menu item it is better (performance wise) to use delegate which would look like this:

$("#wizard").delegate("li", "click", function () console.log( $(this).index() ););In jQuery 1.7+, you should use on. The below example binds the event to the #wizard element, working lượt thích a delegate event:

$("#wizard").on("click", "li", function() console.log( $(this).index() ););

*

*

something like:

$("ul#wizard li").click(function () var index = $("ul#wizard li").index(this); alert("index is: " + index));

*

Take a look at this example.

$("#wizard li").click(function () alert($(this).index()); // alert index of li relative lớn ul parent);
There"s no need lớn require a big library lượt thích jQuery lớn accomplish this, if you don"t want to. Khổng lồ achieve this with built-in DOM manipulation, get a collection of the li siblings in an array, and on click, kiểm tra the indexOf the clicked element in that array.

Xem thêm: Phần Mềm Hệ Thống Là Gì Phần Mềm, Phần Mềm Hệ Thống


const lis = <...document.querySelectorAll("#wizard > li")>;lis.forEach((li) => li.addEventListener("click", () => const index = lis.indexOf(li); console.log(index); );); Step 1 Step 2
const lis = <...document.querySelectorAll("#wizard li")>;document.querySelector("#wizard").addEventListener("click", ( target ) => // Make sure the clicked element is a which is a child of wizard: if (!target.matches("#wizard > li")) return; const index = lis.indexOf(target); console.log(index);); Step 1 Step 2

Or, if the child elements may change dynamically (like with a todo list), then you"ll have to construct the array of lis on every click, rather than beforehand:


const wizard = document.querySelector("#wizard");wizard.addEventListener("click", ( target ) => // Make sure the clicked element is a if (!target.matches("li")) return; const lis = <...wizard.children>; const index = lis.indexOf(target); console.log(index);); Step 1 Step 2
Delegate & Live are easy to lớn use but if you won"t have any more li:s added dynamically you could use event delagation with normal bind/click as well. There should be some performance gain using this method since the DOM won"t have khổng lồ be monitored for new matching elements. Haven"t got any actual numbers but it makes sense :)

$("#wizard").click(function (e) var source = $(e.target); if(source.is("li")) // alert index of li relative to ul parent alert(source.index()); );You could test it at jsFiddle: http://jsfiddle.net/jimmysv/4Sfdh/1/


Yet another way

$("#wizard li").click(function () $($(this),"#wizard"").index(););Demohttps://jsfiddle.net/m9xge3f5/


Thanks for contributing an answer to Stack Overflow!

Please be sure lớn answer the question. Provide details và share 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 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.


Site design / hình ảnh sản phẩm © 2022 Stack Exchange Inc; user contributions licensed under cc by-sa. Rev2022.5.6.42057


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.

Share Tweet Linkedin Pinterest
Previous Post

How to parse json file using jquery

Next Post

Code web html đơn giản

CÙNG CHUYÊN MỤC

jquery file upload (hayageek plugin) error case with json from php

Jquery file upload (hayageek plugin) error case with json from php

28/04/2021
search/filter table rows based on input

Search/filter table rows based on input

28/04/2021
jquery form validation before ajax submit

Jquery form validation before ajax submit

18/03/2021
jquery multiple select dropdown

Jquery multiple select dropdown

28/04/2021
jquery form validation before ajax submit, easy form validation with jquery

Jquery form validation before ajax submit, easy form validation with jquery

25/05/2022
top 17 jquery foreach json array hay nhất 2022

Top 17 jquery foreach json array hay nhất 2022

24/05/2022
how to upload files asynchronously using jquery?

How to upload files asynchronously using jquery?

23/05/2022

22/05/2022

Newsletter

The most important automotive news and events of the day

We won't spam you. Pinky swear.

Chuyên Mục

  • Hỏi Đáp
  • Kiến Thức
  • Sức Khỏe
  • Tử Vi
  • Công Nghệ

News Post

  • Sữa mẹ để ngoài được mấy tiếng

About

Chúng tôi tạo ra trang web nhằm mục đích mang lại kiến thức bổ ích cho cộng đồng, các bài viết được sưu tầm từ nhiều nguồn trên internet giúp mang lại kiến thức khách quan dành cho bạn

©2022 darkedeneurope.com - Website WordPress vì mục đích cộng đồng

Liên Hệ - Giới Thiệu - Nội Quy - Bảo Mật

No Result
View All Result
  • Trang chủ
  • Chuyên mục
    • Hỏi Đáp
    • Kiến Thức
    • Sức Khỏe
    • Tử Vi
    • Công Nghệ
  • Lưu trữ
  • Liên hệ

© 2022 darkedeneurope.com - Website WordPress vì mục đích cộng đồng.