JQuery 多个 If 语句

JQuery Multiple If Statements

我有以下代码块,效果很好:

jQuery(".archive-job_listing-layout").click(function(evt) {
        evt.preventDefault();
        if (!jQuery("body").hasClass('post-type-archive-job_listing')) 
            return;
        console.log("Click " + jQuery(this).data('style'));
        console.log(jQuery(window).width());
        if (jQuery(this).data('style') == "grid" && jQuery(window).width() < 800) {
            jQuery("ul.job_listings").css('display','block');
            jQuery("table#wswp-header-row").hide().remove();
            jQuery(".table_padding").hide().remove();
            return;
        }

        layout_to_table("click");
    })
});

我想做的是添加另一行:

if (!jQuery("body").hasClass('archive tax-job_listing_type'))
return;

但是添加这个会破坏代码。我试过使用 If Else, Or (||) And (&&),但没有任何效果。 如果我用 'archive tax-job_listing_type' 替换 'post-type-archive-job_listing' 代码也可以正常工作,我似乎无法让这两行代码同时工作。

这应该有效:

if(!jQuery("body").hasClass('archive tax-job_listing_type') && !jQuery("body").hasClass('post-type-archive-job_listing'))
    return;

也许用更多的括号分开对你来说是可行的:

if (!(jQuery("body").hasClass('post-type-archive-job_listing')) || !(jQuery("body").hasClass('archive tax-job_listing_type'))) 
        return;

可以使用接受多个选择器的 is()。当多个选择器传递给它时,将表现得像 or

if(!jQuery("body").is('.archive tax-job_listing_type, .post-type-archive-job_listing'))

DEMO