开发者

Make .appendTo() apply to First Element?

I have this code:

<ul id="hello">
    <li>.....</li>
    <li>.....&开发者_JAVA百科lt;/li>
    <li>.....</li>
    <li>.....</li>
    <li>.....</li>
    <li>.....</li>
</ul>

and this Script:

$("<label>The First item</label>").appendTo($('ul#hello li'));

How can I make it so that the .appendTo() function only applies to the first list item.

I've tried the :first-child selector but it doesn't seem to work.

Any help is appreciated.


$("<label>The First item</label>").appendTo($('#hello li:first'));


$("<label>The First item</label>").appendTo($('#hello li').first());

Note: .first() should be a tad faster than :first. Also see: jQuery :first vs. .first()


$('#hello li:first').append('<label>The First item</label>');

or

$('#hello').find('li:first').append('<label>The First item</label>');

or

$('<label>The First item</label>').appendTo($('#hello').find('li:first'));

DEMO

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜