繁体   English   中英

将每个拆分结果与每个类元素进行比较

[英]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 asplit 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM