gpt4 book ai didi

javascript - 如何将 "this"用作选择器的一部分?

转载 作者:行者123 更新时间:2023-11-29 16:20:11 25 4
gpt4 key购买 nike

我试着这样做:

$(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com