如何在 JavaScript 中添加/删除类?
自 element.classList
IE 9 和 Safari-5 不支持,有什么替代的跨浏览器解决方案?
请无框架。
解决方案必须至少适用于IE 9、Safari 5、FireFox 4、Opera 11.5 和 Chrome。
相关帖子(但不包含解决方案):
Since element.classList
is not supported in IE 9 and Safari-5, what's an alternative cross-browser solution?
No-frameworks please.
Solution must work in at least IE 9, Safari 5, FireFox 4, Opera 11.5, and Chrome.
Related posts (but does not contain solution):
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
看看这些单行:
删除类:
添加类(如果已经存在,则不会添加两次):
切换类(如果尚不存在则添加该类,如果存在则将其删除)
就这样!我做了一个测试 - 10000 次迭代。 0.8秒。
Look at these oneliners:
Remove class:
Add class (will not add it twice if already present):
Toggle class (adds the class if it's not already present and removes it if it is)
That's all! I made a test - 10000 iterations. 0.8s.
这是纯 javascript 解决方案中 addClass、removeClass、hasClass 的解决方案。
实际上它来自 http://jaketrent.com/post/addremove-classes-raw-javascript /
Here is solution for addClass, removeClass, hasClass in pure javascript solution.
Actually it's from http://jaketrent.com/post/addremove-classes-raw-javascript/
我刚刚写了这些:
我认为它们适用于所有浏览器。
I just wrote these up:
I think they'll work in all browsers.
最简单的是
element.classList
其中有remove(name)
、add(name)
、toggle(name)
和contains(name) 方法现在是
所有主流浏览器都支持。
对于旧版浏览器,您可以更改
元素.className
。这里有两个助手:The simplest is
element.classList
which hasremove(name)
,add(name)
,toggle(name)
, andcontains(name)
methods and is now supported by all major browsers.For older browsers you change
element.className
. Here are two helper:在没有框架/库的情况下使用类的一种方法是使用属性 Element.className,它“获取并设置指定元素的类属性的值。”(来自 MDN 文档)。
正如 @matías-fidemraizer 在他的回答中已经提到的,一旦你得到了你的类的字符串您可以使用与字符串关联的任何方法来修改它。
这是一个例子:
假设您有一个 ID 为“myDiv”的 div,并且您希望在用户单击它时向其添加类“main__section”,
One way to play around with classes without frameworks/libraries would be using the property Element.className, which "gets and sets the value of the class attribute of the specified element." (from the MDN documentation).
As @matías-fidemraizer already mentioned in his answer, once you get the string of classes for your element you can use any methods associated with strings to modify it.
Here's an example:
Assuming you have a div with the ID "myDiv" and that you want to add to it the class "main__section" when the user clicks on it,
阅读这篇 Mozilla 开发者网络文章:
由于 element.className 属性是字符串类型,因此您可以使用任何 JavaScript 实现中的常规 String 对象函数:
如果要添加类,请首先使用
String.indexOf
中为了检查类是否存在于className中。如果不存在,只需将一个空白字符和新类名连接到该属性即可。如果存在,则不执行任何操作。如果您想删除一个类,只需使用
String.replace
,将“[className]”替换为空字符串。最后使用String.trim
删除element.className
开头和结尾的空白字符。Read this Mozilla Developer Network article:
Since element.className property is of type string, you can use regular String object functions found in any JavaScript implementation:
If you want to add a class, first use
String.indexOf
in order to check if class is present in className. If it's not present, just concatenate a blank character and the new class name to this property. If it's present, do nothing.If you want to remove a class, just use
String.replace
, replacing "[className]" with an empty string. Finally useString.trim
to remove blank characters at the start and end ofelement.className
.修复了 @Paulpro 的解决方案
removeClass
函数被打破了,因为它在重复使用后出现了故障。
`
Fixed the solution from @Paulpro
removeClass
functionwas broken, as it bugged out after repeated use.
`
解决方案是
Shim
.classList
:使用 DOM-shim 或使用下面的 Eli Grey 的 shim
免责声明: 我相信支持的是 FF3.6+、Opera10+、FF5、Chrome、IE8+
The solution is to
Shim
.classList
:Either use the DOM-shim or use Eli Grey's shim below
Disclaimer: I believe the support is FF3.6+, Opera10+, FF5, Chrome, IE8+
埃米尔代码的改进版本(使用trim())
Improved version of emil's code (with trim())
以防万一,如果有人想要为元素构建原型函数,这就是我在需要操作不同对象的类时使用的方法:
使用它们如下:
document.body.addClass('whatever')
或document.body.removeClass('whatever')
除了 body,您还可以使用任何其他元素(div、span、你说出它的名字)
Just in case if anyone would like to have prototype functions built for elements, this is what I use when I need to manipulate classes of different objects:
Use them like:
document.body.addClass('whatever')
ordocument.body.removeClass('whatever')
Instead of body you can also use any other element (div, span, you name it)
添加 css 类:
cssClassesStr += cssClassName;
删除 css 类:
cssClassStr = cssClassStr.replace(cssClassName,"");
添加属性 'Classes':
object. setAttribute("类", ""); //纯添加该属性
删除属性:
object.removeAttribute("class");
add css classes:
cssClassesStr += cssClassName;
remove css classes:
cssClassStr = cssClassStr.replace(cssClassName,"");
add attribute 'Classes':
object.setAttribute("class", ""); //pure addition of this attribute
remove attribute:
object.removeAttribute("class");
一种易于理解的方式:
https://gist.github.com/sorcamarian/ff8db48c4dbf4f5000982072611955a2
A easy to understand way:
https://gist.github.com/sorcamarian/ff8db48c4dbf4f5000982072611955a2