getElementsByClassName vs. jquery

前端 未结 8 577
遇见更好的自我
遇见更好的自我 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:27

    Try this

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

    Try this

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

    You should also just be able to use jQuery's get() method:

    $('.blah').get(9)
    

    jQuery objects also function as indexed arrays as returned elements, so this should also work:

    $('.blah')[9]
    
    0 讨论(0)
  • 2021-01-04 12:32

    Another answer could be:

    $($(data).find('.blah')[9]).html();
    

    When you use [9] it returns a DOM object which doesn't know what function html() is but without [9] it returns a jQuery Object which the html() function is apart of.

    0 讨论(0)
  • 2021-01-04 12:38

    The equivalent of

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

    is to use the :eq pseudo-selector:

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

    or the eq function:

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

    (...and then the html function to set the inner HTML.)

    0 讨论(0)
  • 2021-01-04 12:39
    $('.blah')[9].innerHTML="BLAH";
    

    This should solve your problem

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