![](/img/trans.png)
[英]Make an array A with the result of each value of array B splitted by pipe
[英]Compare each splitted result with each class element
var data = 'sky,sea,earth,moon';
var a = data.split(","), i;
for (i = 0; i < a.length; i++) {
var b = a[i].text();
$('mdcatsitem').each(function(){
if (b == $(this).text()) {$(this).addClass('toggleright');}
});
}
安慰:
Uncaught TypeError: a[i].text is not a function
我需要的:
-将data
分成几部分;
-获取each
部分的文本并将其与each
mdcatsitem
文本进行比较;
-如果匹配-将类添加到mdcatsitem
为什么要在字符串上调用.text()
?
data
是一个定界字符串,在您对它调用.split()
之后, a
就是一个字符串数组:
['sky', 'sea', 'earth', 'moon']
遍历a
,您可以直接将a[i]
作为所需的字符串引用:
var b = a[i];
// b now equals the string, such as 'sky' or 'sea'
将a[i].text()
替换a[i].text()
a[i]
。
a[i]
不是 dom元素, 而是 string 。 a
是split
data
后的数组。
a = ['sky', 'sea', 'earth', 'moon']
使用此代码:
var data = 'sky,sea,earth,moon';
var a = data.split(","), i;
for (i = 0; i < a.length; i++) {
var b = a[i];
$('mdcatsitem').each(function(){
if (b == $(this).text()) {$(this).addClass('toggleright');}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.