javascript 选择附加项 jquery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7349384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:48:47  来源:igfitidea点击:

Select appended item jquery

javascriptjquery

提问by Xavier

How would i select the below item, i do notwant to select every LI of .topbut just the LI i have just created on the append.

我将如何选择以下项目,我希望选择的每一个LI.top只是我刚才在追加创建LI。

How is this possible?

这怎么可能?

$('.top').append('<li><span>' + html + '</span></li>');

采纳答案by Matt Ball

Use the :lastpseudo-class selector.

使用:last伪类选择器

$('.top > li:last')


Alternate option: consider creating the element slightly differently.

替代选项:考虑稍微不同地创建元素。

var $li = $('<li><span>' + html + '</span></li>');
$('.top').append($li);
// you already have the <li> selected, in $li

回答by Richard Dalton

You could do it the other way around using appendTo()

你可以用另一种方式来做 appendTo()

var li = $('<li><span>' + html + '</span></li>').appendTo('.top');

This way you don't have to select it after appending it.

这样您就不必在附加后选择它。

回答by UlfR

You could probably select the last one:

你可能会选择最后一个:

$('.top').last()

See: http://api.jquery.com/last/

见:http: //api.jquery.com/last/