Multiple selector chaining in jQuery?

前端 未结 7 913
迷失自我
迷失自我 2020-11-29 03:23

Normally when I use a class as a selector I try to use an \"id\" selector with it so it does not search through the entire page but only an area where the class would be.

7条回答
  •  青春惊慌失措
    2020-11-29 03:53

    There are already very good answers here, but in some other cases (not this in particular) using map could be the "only" solution.

    Specially when we want to use regexps, other than the standard ones.

    For this case it would look like this:

     $('.myClass').filter(function(index, elem) {
        var jElem = $(elem);
    
        return jElem.closest('#Create').length > 0 || 
               jElem.closest('#Edit').length > 0;
    
    }).plugin(...);
    

    As I said before, here this solution could be useless, but for further problems, is a very good option

提交回复
热议问题