jQuery - select parent 内 selector

jQuery - select parent within the selector

我有 $(html).find('.model').parent().text() 可以按预期工作。有没有办法表达 selector 中的 "parent" 关系? $(html).find('.model:parent').text()$(html).find('.model:parent()').text() 似乎都不起作用。

我将 selector 作为参数传递,因此我希望 selector 直接指向元素。在这种情况下,parent 没有唯一属性,所以我不能 select 它必须先找到 child。我在文档 [1] 中遇到过 parent selector 但不知道如何将它与 class 名称 selector.

[1] https://api.jquery.com/parent-selector/

:parent is not what you're looking for. Perhaps :has() 是最接近您要查找的伪选择器。

用法:

$(html).find('*:has(.model)').text();

或者更具体地说:

$(html).find('h1:has(.model)').text();