में एक तत्व का nth-child संख्या प्राप्त करें मैं अपनी साइट के लिए एक फ़ंक्शन बना रहा हूं जहां मैंने डेटा विशेषता सेट की है जिसमें उस तत्व का nth-child संख्या शामिल है।शुद्ध जावास्क्रिप्ट
मेरे HTML मार्कअप:
<html>
<body>
<section class="hardware">some text, nth-child is one</section>
<section class="hardware">some text, nth-child is two</section>
<section class="hardware">some text, nth-child is three</section>
<section class="hardware">some text, nth-child is four</section>
<section class="hardware">some text, nth-child is five</section>
</body>
</html>
मेरा JavaScript अब तक:
var selector = document.getElementsByClassName('hardware');
for(var i = 0; i <= selector.length; i++) {
var index = selector[i] //get the nth-child number here
selector[i].dataset.number = index;
}
मैं शुद्ध जावास्क्रिप्ट (नहीं jQuery) के साथ एक तत्व के n वें वाले बच्चे नंबर कैसे प्राप्त कर सकते हैं, यह और भी है जावास्क्रिप्ट में संभव है?
जो शुद्ध जावास्क्रिप्ट नहीं है? – bitoiu
@bitoiu हां यह है, लेकिन मैं nth-child संख्या कैसे प्राप्त कर सकता हूं? –
यदि यह jQuery के साथ संभव है, तो यह भी संभव है। याद रखें, jQuery सिर्फ जेएस कार्यों का एक सेट है। यह चीजों को आसान बनाता है, यह अतिरिक्त कार्यक्षमता नहीं जोड़ता है। – Darkwater