[英]Remove class from multiple elements in JavaScript or jQuery
Supposing I have this HTML with multiple elements and class.假设我有这个包含多个元素和类的 HTML。
How can I remove without naming the elements from all the element the class classToRemove
?如何在不命名类
classToRemove
所有元素的情况下删除元素?
Thanks.谢谢。
<html>
<body class="classToRemove">
<div class="classToRemove">
<a class="classToRemove">Link</a>
<div>
</body>
<html>
使用 jQuery 真的很简单$('.classToRemove').removeClass('classToRemove');
Remove class using javascript使用javascript删除类
const removeClass = ($el, className) => {
const _removeClass = function(el) {
if (el.classList) {
el.classList.remove(className);
} else {
el.className = el.className.replace(
new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'),
' ',
);
}
};
if ($el.length === 1) {
_removeClass($el);
} else if ($el.length > 1) {
$el.forEach(function(item) {
_removeClass(item);
});
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.