开发者

JQuery - Cannot select elements based on their index

开发者 https://www.devze.com 2023-03-07 11:47 出处:网络
Lets assume I have the following html: <ul class=\"list\"> <li></li> <li></li>

Lets assume I have the following html:

        <ul class="list">
            <li></li>
            <li></li>
            <li></li>
        </ul>

I cannot do something like thi开发者_如何学编程s: (using .get() and refering by index value:

$(document).ready(function(){
  $('li').get(0).html('some_text')
});

Thanks.


That's because get returns the DOM element, not a jQuery selection. The html method is a jQuery one, not a DOM one. You need eq instead:

$(document).ready(function(){
  $('li').eq(0).html('some_text')
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号