¿Cómo eliminar todas las clases CSS usando jQuery?


En lugar de llamar individualmente a $("#item").removeClass() para cada clase que un elemento pueda tener, ¿hay una sola función a la que se pueda llamar que elimine todas las clases CSS del elemento dado?

Tanto jQuery como JavaScript raw funcionarán.

Author: Peter Mortensen, 2009-09-15

12 answers

$("#item").removeClass();

Llamar a removeClass sin parámetros eliminará todas las clases del elemento.


También puede usar (pero no necesariamente se recomienda, la correcta es la anterior):

$("#item").removeAttr('class');
$("#item").attr('class', '');
$('#item')[0].className = '';

Si no tienes jQuery, entonces esta sería tu única opción:

document.getElementById('item').className = '';
 1349
Author: jimyi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-09-15 11:07:01

Hang on, doesn't removeClass() default to removing all classes if nothing specific is specified? So

$("#item").removeClass();

Lo hará por su cuenta...

 107
Author: da5id,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-09-15 03:38:25

Simplemente establezca el atributo className del elemento DOM real en '' (nada).

$('#item')[0].className = ''; // the real DOM element is at [0]

Edit: Otras personas han dicho que simplemente llamar a removeClass funciona - Probé esto con Google jQuery Playground: http://savedbythegoog.appspot.com/?id=ag5zYXZlZGJ5dGhlZ29vZ3ISCxIJU2F2ZWRDb2RlGIS61gEM ... y funciona. Así que también puedes hacerlo de esta manera:

$("#item").removeClass();
 12
Author: Isaac Waller,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-09-15 03:42:57

Por supuesto.

$('#item')[0].className = '';
// or
document.getElementById('item').className = '';
 10
Author: kangax,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-09-15 03:36:18

El método más corto

$('#item').removeAttr('class').attr('class', '');
 8
Author: Yanni,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-01-21 15:59:00

Heh, vino buscando una respuesta similar. Entonces me di cuenta.

Eliminar Clases específicas

$('.class').removeClass('class');

Digamos si el elemento tiene class = "class another-class"

 8
Author: Shawn Rebelo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-08 18:57:07
$('#elm').removeAttr('class');

La clase attr ya no estará presente en "elm".

 3
Author: uihelp,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-31 13:17:53

Me gusta usar js nativos haz esto, lo creas o no!

1.

// remove all items all class  
const items = document.querySelectorAll('item');
for (let i = 0; i < items.length; i++) {
    items[i].className = '';
}

2.

// only remove all class of first item
const item1 = document.querySelector('item');
item1.className = '';

JQuery ways

  1. $("#item").removeClass();

  2. $("#item").removeClass("class1 ... classn");

 2
Author: xgqfrms,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-06-13 03:56:08

Dado que no todas las versiones de jQuery se crean iguales, puede encontrarse con el mismo problema que hice, lo que significa llamar a $("#item").removeClass (); no elimina la clase. (Probablemente un error)

Un método más confiable es simplemente usar JavaScript sin procesar y eliminar el atributo de clase por completo.

document.getElementById("item").removeAttribute("class");
 1
Author: Vincent,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-20 20:30:31

Intenta con removeClass

Por ejemplo:

var nameClass=document.getElementsByClassName("clase1");
console.log("after", nameClass[0]);
$(".clase1").removeClass();
var nameClass=document.getElementsByClassName("clase1");
console.log("before", nameClass[0]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="clase1">I am Div with class="clase1"</div>
 1
Author: x-rw,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-18 15:34:35

Puedes probar

$(document).ready(function() {
   $('body').find('#item').removeClass();
});

Si tiene que acceder a ese elemento sin nombre de clase, por ejemplo, tiene que agregar un nuevo nombre de clase, puede hacerlo:

$(document).ready(function() {
   $('body').find('#item').removeClass().addClass('class-name');
});

Utilizo esa función en mi projet para eliminar y agregar clases en un constructor html. Buena suerte.

 1
Author: Alessandro Foolish Ciak DAnton,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-03 06:17:10

Tuve un problema similar. En mi caso en los elementos deshabilitados se aplicó que la clase aspNetDisabled y todos los controles deshabilitados tenían colores incorrectos. Por lo tanto, he utilizado jquery para eliminar esta clase en cada elemento/control que no y todo funciona y se ve muy bien ahora.

Este es mi código para eliminar la clase aspNetDisabled:

$(document).ready(function () {

    $("span").removeClass("aspNetDisabled");
    $("select").removeClass("aspNetDisabled");
    $("input").removeClass("aspNetDisabled");

}); 
 -1
Author: Nezir,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-14 14:57:01