简体   繁体   中英

I want to remove class if two classes found in a div, with jQuery function I have tried but not working

$(document).ready(function(){

$(".block").addClass("dock_on_load");

});

$(document).ready(function(){
    var someElem = $("#block-region-side-post");
    if (someElem.is('.block, .hidden')) {
       $('.dock_on_load').removeClass('dock_on_load');
    }
});

I want to remove class dock_on_load if two classes found .block, .hidden and other hand if only block class found add dock_on_load but it is not working

use below code . don't need to use before class '.' in hasClass() .

if ($someElem.hasClass('block') && $someElem.hasClass('hidden')) {

       $('.dock_on_load').remove();  //if you want to remove element 

   // if you wan to remove class from $someElem use below code

      $someElem.removeClass('dock_on_load'); 

 }

use .removeClass(); instead of .remove();

As an alternative to Nishit's answer, you can do the following:

$(document).ready(function(){
    var someElem = $("#block-region-side-post");
    if (someElem.is('.block, .hidden')) {
       $('.dock_on_load').removeClass('dock_on_load');
    }
});

As discussed here , this approach may be slower than using hasClass .

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