简体   繁体   English

如何在jQuery中同时使用OR和.find()?

[英]How can I use both OR and .find() in jQuery?

Here is my code: 这是我的代码:

$('.mycls1 table tr td, .mycls2 table tr td').css('opacity', '.2');

Now I want to use two variables instead of .mycls1 and .mycls2 , Like this: 现在,我想使用两个变量来代替.mycls1.mycls2 ,如下所示:

var mycls1 = $(`.mycls1`),
    mycls2 = $(`.mycls2`);

So I have to use .find() to select that, like this: 因此,我必须使用.find()进行选择,如下所示:

mycls1.find('table tr td').css('opacity', '.2');
mycls2.find('table tr td').css('opacity', '.2');

See? 看到? In this case (using .find() ) forces me to use to two separate lines of code for selecting those two elements. 在这种情况下(使用.find()迫使我使用两行代码来选择这两个元素。

Anyway, I want to know is it possible to use both .find() and OR ( , ) in the same line? 无论如何,我想知道是否可以在同一行中同时使用.find()和OR( , )?


Noted that in my example, declaring two variables and initializing those two elements in them is not effective, but in reality it is. 注意,在我的示例中,声明两个变量并初始化它们中的两个元素并不有效,但实际上是有效的。 So I just need to learn the concept of how can I do that. 所以我只需要学习如何做到这一点的概念。

You can use add() to add elements to collection. 您可以使用add()将元素添加到集合中。

mycls1.add(mycls2)....

This will add the elements from mycls2 . 这将添加mycls2的元素。

 var mycls1 = $(`.mycls1`), mycls2 = $(`.mycls2`); mycls1.add(mycls2).css('opacity', '.2'); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="mycls1"> <table> <tr> <td>1</td> </tr> <tr> <td>2</td> </tr> <tr> <td>3</td> </tr> </table> </div> <div class="mycls2"> <table> <tr> <td>4</td> </tr> <tr> <td>5</td> </tr> <tr> <td>6</td> </tr> </table> </div> 

i think you should try below code 我认为你应该尝试下面的代码

var mycls = $(`.mycls1, .mycls2`);
mycls.find('table tr td').css('opacity', '.2');

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

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