javascript - jquery removeClass() 不删除所有类

标签 javascript jquery

<分区>

我有这样一个元素:

<div class="one two three" id="waterhorse">horse</div>

当我在浏览器控制台中运行这段代码时:

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

我明白了:

[<div id=​"waterhorse" class=​"one two three">​horse​</div>​]

换句话说,它不起作用;它不会删除元素上的任何类。不幸的是,我无法在 jsfiddle 中重现它。

但是,我可以删除特定的类:

$("#waterhorse").removeClass("two");

此外,这将删除所有类:

$("#waterhorse").removeAttr("class");

知道为什么后者可以删除所有类,而前者不能吗?

最佳答案

这似乎是一个已知问题,jQuery 和 jQueryUI 无法很好地协同工作:

Odd issue with jQuery .removeClass() not doing anything

http://bugs.jqueryui.com/ticket/9015

上面的答案是使用 .removeAttr('class') 而不是 .removeClass()

的解决方法

关于javascript - jquery removeClass() 不删除所有类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16355332/

相关文章:

javascript - 经常为 JavaScript 事件处理函数定义的 "event"参数是什么?

jquery - 如何在 CSS 或 jQuery 中创建动态 div 高度

javascript - 使用选项输出而不是选项值

javascript - 如何为不应该执行任何操作的函数编写测试?

javascript - 如何在动态html表中将管道分隔的字符串显示到不同的td中

javascript - React.js - 无法从组件获取特定的 prop

jquery。如何获得 sibling 的高度?

javascript - 获取作为输入文本字段的 Datatables 单元格值

jquery - 改进 val 替换为 jQuery

javascript - 如何避免 jquery 文件之间的冲突