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
Select appended item jquery
提问by Xavier
How would i select the below item, i do notwant to select every LI of .top
but 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 :last
pseudo-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.
这样您就不必在附加后选择它。