用于隐藏某些元素的Javascript代码,但不包含那些包含p元素的元素

Hello i am trying to write javascript not showing some contents when there are specific keywords in dom. But i dont want to display none for the keywords showing in a

element inside those classes. For example

if ($("div[class*='artist']").length || $("div[class*='designer']").length) {
            jQuery("div[class*='artist'], div[class*='designer']").each(function () {
                if (jQuery(this).text().toLowerCase().indexOf("firstword") !== -1 || jQuery(this).text().toLowerCase().indexOf("secondword") !== -1) {
                    jQuery(this).hide();
                    console.log(this);
                }
            });
        }

So i know that this code works very good but i have problem because hiding me more things that i want.

You can exclude the divs with p tag using find function as follows:

if ($("div[class*='artist']").length || $("div[class*='designer']").length) {
    jQuery("div[class*='artist'], div[class*='designer']").each(function () {
        var hasPTag = $(this).find('p');
        if ((jQuery(this).text().toLowerCase().indexOf("firstword") !== -1 || jQuery(this).text().toLowerCase().indexOf("secondword") !== -1) && hasPTag.length == 0) {
            jQuery(this).hide();
            console.log(this);
        }
    });
}

I hope this helps.