JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: วิธีการรับดัชนี (ตำแหน่งภายในกลุ่ม) ของวัตถุด้วย jQuery?
Posted on 20. โพสต์ใน 20. Jun, 2009 by Dragos in Coding , JavaScript & Ajax มิ.ย., 2,009 โดย Dragos ใน การเข้ารหัส, JavaScript และ Ajax
I've spent some time today figuring out how to obtain that index and finally I've just puzzled it out. ฉันได้ใช้เวลาวันนี้ figuring วิธีการรับดัชนีที่และสุดท้ายฉันเพิ่งตื่นออก.
Let's say we have a group of objects all containing the class=”house”. สมมุติว่าเรามีกลุ่มของวัตถุทั้งหมดที่มีระดับ = บ้าน "". Now if we are referencing to the first element within the group and we'd like to find out it's index (well it's obvious in this case), we'd use this piece of code: ตอนนี้ถ้าเราอ้างอิงกับองค์ประกอบแรกในกลุ่มและเราต้องการหาดัชนีก็ (ดีก็เห็นได้ชัดในกรณีนี้) เราต้องการใช้ชิ้นส่วนของรหัสนี้
var indexOfFirstElement=$('.house').index($('.house:first')); //indexOfFirstElement=0; indexOfFirstElement var =$('. บ้าน). ดัชนีบ้าน ($('.: แรก ')) / / = 0 indexOfFirstElement; So, the code works as follows: we're searching within all elements of a group all grouped by the class=”house” ( $('.house') ). ให้โค้ดทำงานดังนี้เรากำลังค้นหาภายในองค์ประกอบทั้งหมดของกลุ่มการจัดกลุ่มทุกชั้น = บ้าน "" ($ บ้าน. ( '')). Now we are looking for the index if an element within this group, in our case it's the first element of the group ( $('.house') .index($('.house:first')) ). ขณะนี้เรากำลังมองหาดัชนีถ้าองค์ประกอบภายในกลุ่มนี้ในกรณีของเราเป็นธาตุแรกของกลุ่ม ($ บ้าน. ( '') ดัชนี. บ้าน ($('.: ครั้งแรก))).
Here's another example to help you better understand how index() works: นี่ตัวอย่างเพื่อช่วยให้คุณเข้าใจวิธีการที่ดีกว่า ดัชนี () งานอื่น:
$('.house').each(function(i){ $ บ้าน. ( ''). แต่ละ (ฟังก์ชัน (i) ( if(i==3) alert($('.house').index(this)); //alerts 3, supposing there are 3 or more elements with class="house" ถ้า (i == 3 บ้าน ($('.) เตือน ') ดัชนี. (นี้)) / / เตือน 3 ต่างมีน้อย 3 องค์ประกอบด้วยบ้านชั้น = "" }); ));
So basically, if you need to find out the index of an element within a group, you should first specify the group and then pass the object you need to find the index of as a parameter to the index() function. ดังนั้นโดยทั่วไปถ้าคุณต้องดูดัชนีขององค์ประกอบภายในกลุ่มขั้นแรกคุณควรระบุกลุ่มแล้วผ่านวัตถุที่คุณต้องการหาดัชนีการเป็นพารามิเตอร์ให้ ดัชนี ฟังก์ชัน ().
Related posts: บทความที่เกี่ยวข้อง:
- JavaScript: GIFless animation. JavaScript: ภาพเคลื่อนไหว GIFless. Animate images,logos with jQuery ภาพชีวิตโลโก้กับ jQuery
- JavaScript: What if jQuery animation doesn't fire/start? JavaScript: ถ้าเคลื่อนไหว jQuery จะไฟไม่ / เริ่ม?
- JavaScript: Where do I Find All Properties for All HTML Elements ? JavaScript: ฉันจะหาคุณสมบัติทั้งหมดสำหรับทุกองค์ประกอบ HTML ที่ไหน
- JavaScript: Send function as a parameter to another function (callbacks) JavaScript: ส่งหน้าที่เป็นพารามิเตอร์ทำงาน (callbacks อื่น)
- JavaScript: Get anchor from URL JavaScript: Get ทอดสมอจาก URL












































