[英]Converting string to javascript words
I am using this to find all elements inside some div: 我正在使用它来查找某个div中的所有元素:
var counts = {}
var element = 'div';
$('.workspace').find('*').each(function() {
var tag = this.tagName.toLowerCase();
counts[tag] = counts[tag] || 0;
counts[tag] ++;
});
Now I am checking does element exist in counts: 现在,我正在检查元素是否存在计数:
if(el in counts)
And if it does I am need to get number of divs with: counts.div
but since div is string in var element
I need to use counts.element
and there I get error undefined
because it can't find element its like I said counts.'elements'
. 如果确实如此,我需要得到具有div的数量:
counts.div
但由于格是string in var element
我需要使用counts.element
还有我得到的error undefined
,因为它无法找到元素它就像我说的counts.'elements'
。
Basically when i use counts.div or counts.header etc I get number of how many divs are there inside some element. 基本上,当我使用counts.div或counts.header等时,我得到某个元素内有多少个div。 How can I accomplish this?
我该怎么做?
Are you looking for counts[element]
? 您是否在寻找
counts[element]
? This is called bracket notation . 这称为括号符号 。 You already used it when accessing
counts[tag]++
. 访问
counts[tag]++
时,您已经使用了它。
You are already using jQuery, use jQuery()
, .filter()
您已经在使用jQuery,请使用
jQuery()
.filter()
var counts = $(".workspace *");
var element = "div";
var len = counts.filter(element).length;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.