[英]Count html5 data attributes
I'm trying to count how many elements (list) have the html data attribute of 'data-facet', but I'm undefined. 我试图计算有多少元素(列表)具有'data-facet'的html数据属性,但是我不确定。
Here is what I have tried: 这是我尝试过的:
$('li').data('facet').length;
Any ideas where I've gone wrong? 有什么想法我做错了吗?
If data('facet')
represents an integer, then that would explain the undefined
result (because you can't run .length
on an integer). 如果
data('facet')
表示一个整数,则这将解释undefined
结果(因为您不能对整数运行.length
)。 You can fix this by doing $('li').data('facet').toString().length;
您可以通过执行
$('li').data('facet').toString().length;
来解决此问题$('li').data('facet').toString().length;
instead, but that's not going to get you the result you want (instead, it will give you the length of the data-facet
value of the first li
element). 相反,但这并不能为您带来所需的结果(相反,它将为您提供第一个
li
元素的data-facet
值的长度)。
To get the desired count of li
s, do this: 要获得所需的
li
计数,请执行以下操作:
$('li[data-facet]').length
That will give you the number of li
element that have the data-facet
attribute. 这将为您提供具有
data-facet
属性的li
元素的数量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.