“Classlist” Code-Antworten

Classlist

const div = document.createElement('div');
div.className = 'foo';

// our starting state: <div class="foo"></div>
console.log(div.outerHTML);

// use the classList API to remove and add classes
div.classList.remove("foo");
div.classList.add("anotherclass");

// <div class="anotherclass"></div>
console.log(div.outerHTML);

// if visible is set remove it, otherwise add it
div.classList.toggle("visible");

// add/remove visible, depending on test conditional, i less than 10
div.classList.toggle("visible", i < 10 );

console.log(div.classList.contains("foo"));

// add or remove multiple classes
div.classList.add("foo", "bar", "baz");
div.classList.remove("foo", "bar", "baz");

// add or remove multiple classes using spread syntax
const cls = ["foo", "bar"];
div.classList.add(...cls);
div.classList.remove(...cls);

// replace class "foo" with class "bar"
div.classList.replace("foo", "bar");
Mehedi Islam Ripon

Classlist


// use the classList API to remove and add classes
div.classList.remove("foo");
div.classList.add("anotherclass");
Prickly Parrot

.Classliste

// .classList method returns an array of the classes attached to the element it is called with.
document.getElementById("myDIV").classList
// It returns
// [ 'className1', 'className2', ... ]
subodhk

Ähnliche Antworten wie “Classlist”

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen