getElementsByClassName vs. jquery

前端 未结 8 578
遇见更好的自我
遇见更好的自我 2021-01-04 12:09

If my original function was:

document.getElementsByClassName(\'blah\')[9].innerHTML = \'blah\';

...how would I change that so I get that sa

相关标签:
8条回答
  • 2021-01-04 12:43

    try the following

    $('.blah').eq(9).html('blah');
    
    0 讨论(0)
  • 2021-01-04 12:44

    See what you are looking for is :eq():

    $('.blah').eq(9).html('blah');
    

    because :eq() is 0 indexed,so :eq(9) will find the item at 10th index.

    .eq() jQuery doc

    There is :nth-child() function too:

    $('.blah:nth-child(10)').html('blah');
    

    because :nth-child() is 1 indexed so you have to give place 10th position there.

    :nth-child() jQuery doc

    from the docs:

    Because jQuery's implementation of :nth- selectors is strictly derived from the CSS specification, the value of n is "1-indexed", meaning that the counting starts at 1. For other selector expressions such as :eq() or :even jQuery follows JavaScript's "0-indexed" counting. Given a single containing two

  • s, $('li:nth-child(1)') selects the first
  • while $('li:eq(1)') selects the second.

  • 0 讨论(0)
提交回复
热议问题