简体   繁体   中英

Having trouble removing classes from multiple selectors using removeClass()

Here is my HTML(below). I just want to remove the angular classes: ng-untouched ng-valid from the HTML page through a script. I have written simple JavaScript code below that I thought would achieve this. But when I run the script, I still the Angular classes on my page. I am not sure what I could be missing or is there a different method to this? I tried to follow what they have mentioned here: Removing multiple classes (jQuery) .

<div class=“mainClass”>
    <form action="https://www.google.com/" class="ng-untouched ng-valid">
    </form>
</div>

JavaScript

$("mainClass, form").removeClass(".ng-untouched .ng-valid");

$(".mainClass > form").removeClass("ng-untouched ng-valid");

I think you aren't selecting the class correctly - so try adding the period to the element selector and remove the periods from the class names you're trying to drop.

$("form", .mainClass).removeClass("ng-untouched ng-valid");

note the addition of the period before mainClass and the removal of the periods before each "ng"

Two issues with your code:

  • The arguments to removeClass should have only the class name, not the selector, ie ng-untouched ng-valid .
  • Your double quotes are the wrong character for mainClass

get rid of the dots in front of your classnames.

$(".mainClass, form").removeClass("ng-untouched ng-valid");
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        div {
            font-size: 50px;
        }
        
        .Text1 {
            font-size: 30px;
        }
    </style>
</head>

<body>
    <div id="Text1" class="Text1">Hello World</div>
</body>
<script>
    let Div1 = document.querySelector("#Text1");
    Div1.classList.remove("Text1");
</script>

</html>

also remove the dot from removeClass() method

ng-untouched ng-valid

$(".mainClass form").removeClass("ng-untouched ng-valid");

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