从性能的Angular 来看,是否有一种确定元素是否分配了类的首选方法?
$('#foo').hasClass('bar');
或
$('#foo').is('.bar');
从性能的Angular 来看,是否有一种确定元素是否分配了类的首选方法?
$('#foo').hasClass('bar');
或
$('#foo').is('.bar');
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:
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
应该更快.