简体   繁体   English

如果要在div中找到两个类,我想删除类,但我尝试过但无法使用jQuery函数

[英]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 如果要找到两个类.block,.hidden和另一只手,如果只找到块类,我想删除类ock_on_load,则添加dock_on_load但它不起作用

use below code . 使用下面的代码。 don't need to use before class '.' 无需在课程“。”之前使用。 in hasClass() . 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(); 使用.removeClass(); instead of .remove(); 而不是.remove();

As an alternative to Nishit's answer, you can do the following: 作为Nishit答案的替代方法,您可以执行以下操作:

$(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 . 作为讨论在这里 ,这种方法可能比使用慢hasClass

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想用@keyframes 将 animation 添加到 div 中。 我基本上已经尝试了我所知道的一切 - i want to add an animation to the div with @keyframes. I have tried basically everything i know 如何使用jQuery为元素设置类?不想添加/删除类 - How can I set the class for an element using jQuery? Don't want to add/remove classes 我必须通过jQuery删除父div - I have to remove parent div through jQuery 我如何使用切换和 function 从前一个元素中删除 class 仅使用 JavaScript 我尝试过不同的方式? - how i can use toggle and function that remove the class from the previous element together only using JavaScript i have tried to Different ways? React 一直在说。map 不是 function 并且我尝试过的没有任何工作? - React keeps saying .map is not a function and nothing I have tried is working? Jquery function 只在第一个元素上工作,我想在所有类上工作 - Jquery function is working just on first element, I want to work on all classes jQuery FAQ功能切换不起作用,因为我想要它 - Jquery FAQ function toggling not working as I want it to 使用 react-bootstrap 时出现模块未找到错误。 已尝试所有步骤 - I have module not found error while working with react-bootstrap. Have tried all the steps jQuery添加一个类-我尝试过的所有方法均会在单击时删除该类 - jQuery adding a class - all methods I've tried remove the class on click 我想在页面刷新时更改背景图像图案。 我已经尝试了许多代码,但没有按要求工作 - I want to change background image pattern on page refresh. i have tried many codes but not working as i required
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM