我试着这样做:
$(this + ' li:not(:nth-last-child(2)):not(:last-child)').hide()
完整代码示例:
$('.comments').each(function(){
$(this + ' li:not(:nth-last-child(2)):not(:last-child)').hide()
var template = $('#expand').html();
$(this).prepend(template)
});
我需要将它作为一个“每个”函数来运行,因为我想稍后放入条件。
最佳答案
试试这个:
$('li:not(:nth-last-child(2)):not(:last-child)', this).hide();
来自 docs
jQuery( selector [, context] )
selector - A string containing a selector expression
context - A DOM Element, Document, or jQuery to use as context
所以你可以使用this
作为上下文参数:
$('.comments').each(function(){
$('li:not(:nth-last-child(2)):not(:last-child)', this).hide()
var template = $('#expand').html();
$(this).prepend(template)
});
关于javascript - 如何将 "this"用作选择器的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11170109/