简体   繁体   中英

Can I create a selector with multiple elements using Jquery closest()

I have a listener for click events from which I need to exclude some elements.

Right now my list is growing, so I'm looking for a better way to "bundle" multiple elements in a selector.

This is what I have:

$(document).on('click tap', function(event) {               
    if ($(event.target).closest('div:jqmData(panel="popover")').length > 0 ||
        $(event.target).closest('div.pop_menuBox').length > 0 ||
        $(event.target).closest('.toggle_popover').length > 0 ) ||
        $(event.target).closest('.ui-selectmenu').length > 0 {
          return; 
    }
    // do stuff
});

Is there a better way to exclude these elements?

Thanks for help!

You can specify CSS selectors, which means: you can use the comma to specify two or more selectors:

if($(event.target).closest('div:jqmData(panel="popover"), div.pop_menuBox, .toggle_popover, .ui-selectmenu').length > 0) {
    return; 
}

Use a comma.

if ($(event.target)
     .closest('div:jqmData(panel="popover"), div.pop_menuBox, .toggle_popover, .ui-selectmenu').length > 0) {
     return; 
}

根据jquery文档,您可以为最近的选择器提供多个选择器: http//api.jquery.com/closest/

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