Javascript 将jquery代码应用于多个类

Javascript 将jquery代码应用于多个类,javascript,jquery,class,Javascript,Jquery,Class,我只将这段代码应用于一个类 function replaceSF(foo,bar) { j$('.labelCol').children().each(function(){ console.log('i am on children each for labelCol'); console.log('foo: '+foo); console.log('bar: '+bar); console.log('jsthis.text: '+j$(this).text(

我只将这段代码应用于一个类

function replaceSF(foo,bar)
{
j$('.labelCol').children().each(function(){ 
    console.log('i am on children each for labelCol');
    console.log('foo: '+foo);
    console.log('bar: '+bar);
    console.log('jsthis.text: '+j$(this).text());
    if(j$(this).text() == foo)
    {      
        j$(this).html(''+bar);    
    }
    else if(j$(this).text().trim().replace('\*','').replace('\n','') == foo)
    {
        j$(this).html(j$(this).html().replace(foo,''+bar));
    }
    });  
    j$('.labelCol').each(function(){   
    if(j$(this).text() == foo )
    {      
        j$(this).html(''+bar);   
    }
    }); 
}
但我需要将此代码用于大约10个类。最好的方法是什么? 我试图将此函数重写为
replaceSF(className,foo,bar)
,但没有得到任何结果


谢谢。

添加更多类似下面的课程

j$('.labelCol,.anotherclassname,.anotherclassname1').children().each(function(){

您可以将选择器修改为类似以下内容:

j$('.labelCol, .labelCol2, .labelCol3, .labelCol4, .labelColn').children().each(function(){ 

要按照自己的意愿编写函数,可以采用以下方法:

function replaceSF(arrayOfClassNames,foo,bar){
    j$('.' + arrayOfClassNames.join(', .')).children() /* and so on… */

    /* which would form the CSS-style selector:
    j$('.labelCol, .secondClassName, .anotherClassName').children()
    */
}
将函数调用为:

replaceSF(['labelCol','secondClassName', 'anotherClassName'], 'foo', 'bar');

您应该在尝试“将此函数重写为
replaceSF(className,foo,bar)
”的地方发布代码,也就是说,您可以像编写CSS规则一样编写jQuery选择器。