从性能的Angular 来看,是否有一种确定元素是否分配了类的首选方法?

$('#foo').hasClass('bar');

$('#foo').is('.bar');

推荐答案

Update:

I committed a test following a comment and four upvotes to very comment. It turns out that what I had said is the correct answer. Here is the result:

enter image description here

http://jsperf.com/hasclass-vs-is-so


The is is multi-purpose, you can for example do is('.class'), is(':checked'), etc which means is has more to do where is hasClass is limited which only checks for a class being set or not.

因此,如果任何级别的性能都是您的首要任务,那么hasClass应该更快.

Jquery相关问答推荐

在 Mastodon 中将 jQuery 添加到 Rails 6

ASP.NET Core 6 jQuery 验证不起作用

如何在外部JS文件中使用带有参数的laravel路由

获取 html 元素的 onclick 事件的 data-* 属性

使用 jQuery $.ajax() 时如何使用 GET 在请求正文中发送数据

JQuery通过类名获取所有元素

jQuery javascript 正则表达式 将
替换为 \n

如何使用 jQuery 清空输入值?

如何使用 JQuery 发布 JSON 数据?

如何使用 jquery 正确格式化货币?

jQuery中追加的相反

如何使所有浏览器都支持 ?有什么 Select 吗?

如何阻止 Chrome 进入调试模式?

如何使用 Twitter Bootstrap 自动关闭alert

与 C# HashSet 等效的 JavaScript 是什么?

如何通过jQuery函数仅获取直接子元素

图片转Base64

CORS - 如何预检httprequest?

jquery $(window).height() 正在返回文档高度

Bootstrap:在 Modal 中打开另一个 Modal