使用jquery基于td值添加类

使用jquery基于td值添加类,jquery,Jquery,我有以下方式的行 <table id="tblInbox"> <tbody> <tr class='active'><td>Daily Price</td><td>1</td><td>Santosh</td></tr> <tr class='active'><td>Daily Price</td><td>2

我有以下方式的行

<table id="tblInbox">
<tbody>    
    <tr class='active'><td>Daily Price</td><td>1</td><td>Santosh</td></tr>
    <tr class='active'><td>Daily Price</td><td>2</td><td>Star</td></tr>
    <tr><td>Weekly Price</td><td>3</td><td>Zakeer</td></tr>
    <tr><td>Weekly Price</td><td>4</td><td>Ubed</td></tr>
    <tr><td>Weekly Price</td><td>5</td><td>xyz/td></tr>
    <tr><td>Daily Price</td><td>6</td><td>India</td></tr>
    <tr><td>Daily Price</td><td>7</td><td>Australia</td></tr>
</tbody>
</table>
在按下“下一步”按钮时,我想通过将类删除到现有行中,只将类添加到下一个20或10(无论计数是多少)行中,这些行的第一个td值为“每日价格”。我的代码是这样的

$(document).on('click','#nextPage',function(){ // Clicking Next Button.
    // Remove class to already existing class rows.
    $('.active').removeClass('active');     
    // Confused here. 
 });
你们能推荐我吗,伙计们

您可以使用和

$(document).on('click','#nextPage',function(){ // Clicking Next Button.  
    $('.active :first-child:contains("Daily Price")').removeClass('active').addClass('inactive'); 
});
$(document).on('click','#nextPage',function(){ // Clicking Next Button.  
    $('tr :first-child:contains("Daily Price")').removeClass('active').addClass('inactive'); 
});
$(document).on('click','#nextPage',function(){ // Clicking Next Button.  
    $('tr :first-child:contains("Daily Price")').each(function(){       
       if($(this).parent()[0].className === '')
           $(this).parent().addClass('active');
        else
        if($(this).parent()[0].className == 'active')        
           $(this).parent().removeClass('active');  

    });
});
$(document).on('click','#nextPage',function(){ // Clicking Next Button.  
    $('tr :first-child:contains("Daily Price")').each(function(){       
        $(this).parent()[0].className = $(this).parent()[0].className === "active" ? "" : "active";
    });
});
要进行检查而不考虑tr类,可以在选择器中使用tr,而不是.active

$(document).on('click','#nextPage',function(){ // Clicking Next Button.  
    $('.active :first-child:contains("Daily Price")').removeClass('active').addClass('inactive'); 
});
$(document).on('click','#nextPage',function(){ // Clicking Next Button.  
    $('tr :first-child:contains("Daily Price")').removeClass('active').addClass('inactive'); 
});
$(document).on('click','#nextPage',function(){ // Clicking Next Button.  
    $('tr :first-child:contains("Daily Price")').each(function(){       
       if($(this).parent()[0].className === '')
           $(this).parent().addClass('active');
        else
        if($(this).parent()[0].className == 'active')        
           $(this).parent().removeClass('active');  

    });
});
$(document).on('click','#nextPage',function(){ // Clicking Next Button.  
    $('tr :first-child:contains("Daily Price")').each(function(){       
        $(this).parent()[0].className = $(this).parent()[0].className === "active" ? "" : "active";
    });
});
要在行之间切换类,需要使用每个行进行迭代

$(document).on('click','#nextPage',function(){ // Clicking Next Button.  
    $('.active :first-child:contains("Daily Price")').removeClass('active').addClass('inactive'); 
});
$(document).on('click','#nextPage',function(){ // Clicking Next Button.  
    $('tr :first-child:contains("Daily Price")').removeClass('active').addClass('inactive'); 
});
$(document).on('click','#nextPage',function(){ // Clicking Next Button.  
    $('tr :first-child:contains("Daily Price")').each(function(){       
       if($(this).parent()[0].className === '')
           $(this).parent().addClass('active');
        else
        if($(this).parent()[0].className == 'active')        
           $(this).parent().removeClass('active');  

    });
});
$(document).on('click','#nextPage',function(){ // Clicking Next Button.  
    $('tr :first-child:contains("Daily Price")').each(function(){       
        $(this).parent()[0].className = $(this).parent()[0].className === "active" ? "" : "active";
    });
});
也可以使用条件运算符

$(document).on('click','#nextPage',function(){ // Clicking Next Button.  
    $('.active :first-child:contains("Daily Price")').removeClass('active').addClass('inactive'); 
});
$(document).on('click','#nextPage',function(){ // Clicking Next Button.  
    $('tr :first-child:contains("Daily Price")').removeClass('active').addClass('inactive'); 
});
$(document).on('click','#nextPage',function(){ // Clicking Next Button.  
    $('tr :first-child:contains("Daily Price")').each(function(){       
       if($(this).parent()[0].className === '')
           $(this).parent().addClass('active');
        else
        if($(this).parent()[0].className == 'active')        
           $(this).parent().removeClass('active');  

    });
});
$(document).on('click','#nextPage',function(){ // Clicking Next Button.  
    $('tr :first-child:contains("Daily Price")').each(function(){       
        $(this).parent()[0].className = $(this).parent()[0].className === "active" ? "" : "active";
    });
});
试试这个

$("#tblInbox tbody tr").children("td:contains('Daily Price')").parent().addClass('active')

比如说@ArunPJohny,谢谢你的提琴。但是,阿伦,你误解了我的问题。我将只显示加载了“活动”类的行,单击“下一步”,我必须检查所有带有该td文本的行,并添加“活动”类。我已经更改了小提琴。请检查一下。那样的话,它也可以正常工作,不是吗it@ArunPJohny,不,亲爱的,它将显示该类的现有行。如果我保持$('.active').removeClass('active')。我想把这个类添加到包含“Daily Price”的td文本中。我将通过简单的$('.active')删除该类;通过检查所有行,我需要添加具有td text条件的类。实际上,您是用绿色显示行。那很好。但是,当您单击next时,它必须将现有类删除到前两行,并使用该文本检查其他行。我一次只能显示2行。我添加了新的解决方案,它是针对tr的,不需要查找。活动,这是您想要的吗?其他小部分是正确的。首先,您将以绿色显示前两行。那很好。单击“下一步”时,包含该td文本的下两行应为绿色。已经存在的绿色行应该是正常的。正在删除“活动”类。我一次只能展示两排。工作很有魅力。谢谢你,阿迪尔