如何删除 .class1、.class2、... 或 classN?

发布于 2025-01-01 21:47:20 字数 211 浏览 2 评论 0原文

我们有这个类名

.lightbox620
.lightbox400
..
.lightbox200

,我们应用于正文页面,它决定了它的宽度...

所以我需要删除这个类,

我如何 $('body').removeClass('ligbox{any}')< /代码> ???

we have this classnames

.lightbox620
.lightbox400
..
.lightbox200

That we apply to the body page and it determines its width...

so i need to remove this class,

how can i $('body').removeClass('ligbox{any}') ???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟─花易冷 2025-01-08 21:47:20

如果这是您设置为 的唯一类,则删除所有类。

$("body").removeClass();

// or
document.body.className = "";

如果不是,请采用普通 DOM 方式,使用正则表达式将类名从字符串中去除。

document.body.className = document.body.className.replace(/\blightbox\d+/, "");

这里 jQuery 的方式有点复杂:

$("body").removeClass(function (index, oldClass) {
   var matches = oldClass.match(/\blightbox\d+/) || [];
   return matches[0];
});

If that's the only class that you set to <body>, then remove all the classes

$("body").removeClass();

// or
document.body.className = "";

If not, go the plain DOM way, use a regular expression to strip the class name out of the string.

document.body.className = document.body.className.replace(/\blightbox\d+/, "");

The jQuery way is a bit more complicated here:

$("body").removeClass(function (index, oldClass) {
   var matches = oldClass.match(/\blightbox\d+/) || [];
   return matches[0];
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文