简体   繁体   中英

Can I put multiple jquery selector in the same command

I have two command in jQuery wherein all elements are same but not one.

Commands :

$("#abc > .className1 > .className2 > .className3");
$("#pqr > .className1 > .className2 > .className3");

Can I join above two commands and make it one ?

Any inputs in this regard are appreciated.

On选项是使用find方法:

$('#abc, #pqr').find('> .className1 > .className2 > .className3')

是的,您可以使用逗号将多个选择器分开

$("#abc > .className1 > .className2 > .className3, #pqr > .className1 > .className2 > .className3");

是,带有多个选择器(“ selector1,selector2,selectorN”) https://api.jquery.com/multiple-selector/

$("#abc > .className1 > .className2 > .className3, #pqr > .className1 > .className2 > .className3");

You can store children selector in variable and use it in jquery selector.

var childs = "> .className1 > .className2 > .className3";
$("#abc"+ childs+", #pqr"+ childs)
// Or
$("#abc"+ childs).add("#pqr"+ childs)

 var childs = "> .className1 > .className2 > .className3"; $("#abc"+ childs).add("#pqr"+ childs).css("color", "red"); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="abc"> <div class="className1"> <div class="className2"> <div class="className3">className3</div> </div> </div> </div> <div id="pqr"> <div class="className1"> <div class="className2"> <div class="className3">className3</div> </div> </div> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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