简体   繁体   English

复杂选择器中的jQuery OR选择器

[英]jquery OR selector in complex selector

I have the following selector: 我有以下选择器:

$(".product_tile:nth-child(6n) .tile_back select[name='crust']")

It finds every 6th product_tile, then gets the child div with the tile_back class, and then inside that the select box with the name crust. 它会找到每6个product_tile,然后获取带有tile_back类的子div,然后在其中找到名为crust的选择框。 I now need to modify this so that it finds one of two select boxes - either crust or base. 现在,我需要对其进行修改,以便找到两个选择框之一-地壳或基础。

I know that in a basic selector, I could use a comma: 我知道在基本选择器中,我可以使用逗号:

$("select[name='crust'], select[name='base']")

But if I use the comma-delimited selector inside the original selector, will it know that the comma only applies to the last part of it, or will it see it as "find every 6th product_tile's tile_back's crust select box OR find any select box named base"? 但是,如果我在原始选择器中使用逗号分隔的选择器,它将知道逗号仅适用于它的最后一部分,还是会看到它为“在第6个product_tile的tile_back的外壳选择框内查找,或找到任何名为基础”? And if it's the latter, how can I write the selector so that it sees it as "find every 6th product_tile's tile_back's crust select box OR find every 6th product_tile's tile_back's base select box"? 如果是后者,我该如何编写选择器,使选择器将其视为“找到每6个product_tile的tile_back的外壳选择框,或找到每6个product_tile的tile_back的基本选择框”?

You can specify the context for it to search.. using the context selector like this 您可以使用上下文选择器指定要搜索的上下文

$("select[name='crust'], select[name='base']",'.product_tile:nth-child(6n) .tile_back')

is initially the same as 最初与

$('.product_tile:nth-child(6n) .tile_back').find("select[name='crust'], select[name='base']");

Where it will find all selects with name=crust or name=base under .product_tile:nth-child(6n) 将在.product_tile:nth-​​child(6n)下找到名称为crust或名称为base的所有选择的位置

You won't be able to put the logic in the selector. 您将无法将逻辑放入选择器。 You'll either have to have both full selectors with the comma: 您要么必须将两个完整的选择器都用逗号括起来:

$(".product_tile:nth-child(6n) .tile_back select[name='crust'], product_tile:nth-child(6n) .tile_back select[name='base']")

or use one selector to match until .tile_back and filter with a second selector: 或使用一个选择器进行匹配,直到.tile_back并使用第二个选择器进行过滤:

$(".product_tile:nth-child(6n) .tile_back").find("select[name='crust'], select[name='base']")

不,只需在逗号后面添加开头部分:

$(".product_tile:nth-child(6n) .tile_back select[name='crust'], .product_tile:nth-child(6n) .tile_back select[name='base']")

.add()方法将所选元素添加到结果集中:

$(".product_tile:nth-child(6n) .tile_back").add("select[name='crust']").add("select[name='base']");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM