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');}
});
}
Console:
Uncaught TypeError: a[i].text is not a function
What I need:
- split data
into parts;
- get text of each
part and compare it to text of each
mdcatsitem
;
- if matched - add a class to mdcatsitem
Why are you calling .text()
on a string?
data
is a delimited string, and after you call .split()
on it then a
is simply an array of strings:
['sky', 'sea', 'earth', 'moon']
When looping over a
, you can just reference a[i]
directly as the string you want:
var b = a[i];
// b now equals the string, such as 'sky' or 'sea'
Replace a[i].text()
with a[i]
.
a[i]
is not a dom element but a string . a
is an array after you split
the data
.
a = ['sky', 'sea', 'earth', 'moon']
Use this code:
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');}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.