Javascript jQuery:计算那些没有属性的元素
统计Javascript jQuery:计算那些没有属性的元素,javascript,jquery,class,content-length,Javascript,Jquery,Class,Content Length,统计#mytable中tds的数量 如何在没有属性class(没有任何类)的tds中计数 上面将捕获不带class属性的元素,下面将捕获不带class属性或带class=”“属性的元素 但是,您可能仍然会遇到类似class=”“的问题,因此使用T.J.Crowder已经展示过的过滤函数会更安全 $('#mytable td:not([class]), #mytable td[class=""]').length; 上面将捕获不带class属性的元素,下面将捕获不带class属性或带class=
#mytable
中td
s的数量
如何在没有属性class
(没有任何类)的td
s中计数
上面将捕获不带class
属性的元素,下面将捕获不带class
属性或带class=”“
属性的元素
但是,您可能仍然会遇到类似class=”“
的问题,因此使用T.J.Crowder已经展示过的过滤函数会更安全
$('#mytable td:not([class]), #mytable td[class=""]').length;
上面将捕获不带class
属性的元素,下面将捕获不带class
属性或带class=”“
属性的元素
但是,您可能仍然会遇到类似class=”“
的问题,因此使用T.J.Crowder已经展示过的过滤函数会更安全
$('#mytable td:not([class]), #mytable td[class=""]').length;
虽然plalx的答案可能有效,但它很微妙,但完全有可能做到:
$("#mytable td").filter(function() {
//the replaces just trim the value
return this.className.replace(/^\s+/, '').replace(/\s+$/, '') === "";
}).length;
|虽然普拉克斯的答案可能有效,但很微妙,但完全有可能做到:
$("#mytable td").filter(function() {
//the replaces just trim the value
return this.className.replace(/^\s+/, '').replace(/\s+$/, '') === "";
}).length;
|谢谢你指出这一点!看起来我的问题比我想象的更聪明:D@Haradzieniec如果你有类似于
class=”“
“与他的选择器匹配”的内容,请确保也修剪类名,但无论如何,我已经更新了答案。谢谢你指出这一点!看起来我的问题比我想象的更聪明:D@Haradzieniec如果你有类似于class=”“
“将匹配他的选择器”的内容,那么请确保也修剪类名,因为它应该是不匹配的,但无论如何,我已经更新了答案。
<td class=""></td>
var countWithNoClasses = $("#mytable td").filter(function() {
return $.trim(this.className) === "";
}).length;