如何在jQuery中找到包含的标题eq位置

如何在jQuery中找到包含的标题eq位置,jquery,Jquery,我有一个表,我需要知道eq标题的位置,同时包含单词“this”,然后将类添加到同一行的标记中 <table> <tr> <th>Header1</th> <th>Header2this</th> //catch you in 1 <th>Header3this</th> //catch you in 2 </tr> &

我有一个表,我需要知道eq标题的位置,同时包含单词“this”,然后将类添加到同一行的标记中

<table>
    <tr>
        <th>Header1</th>
        <th>Header2this</th> //catch you in 1
        <th>Header3this</th> //catch you in 2
    </tr>
    <tr>
        <td>Row1</td>
        <td>Row1</td> //Add class
        <td>Row1</td> //Add class
    </tr>   

谢谢你的帮助

一旦你把它分解成几个部分,这就相当简单了:

首先,您需要在文本中找到包含“this”的所有th元素:

$('#sometable').find("th").filter(function () {
    return $(this).text().indexOf('this') > 0;
})
然后,对于其中的每一个,您需要计算出它们的索引位置,并向td添加一个索引等于它的类:

.each(function() {
    var $table = $('#sometable');
    $table.find('td').eq($table.find("th").index(this)).addClass('class');
});
因此,把所有这些放在一起:

$('#sometable').find("th").filter(function () {
    return $(this).text().indexOf('this') > 0;
}).each(function() {
    var $table = $('#sometable');
    $table.find('td').eq($table.find("th").index(this)).addClass('class');
});

和。

您可以使用indexOf查看变量是否包含指定的字符串

var text = $(this).text();
if (text.indexOf("this") != -1) {
您可以使用以下方法将类添加到每x行中:

:nth-child(3n+1)
以下是一个工作版本:

你试过什么吗?你能展示你的努力吗?你所说的头球位置是什么意思?@putvande你不需要评论这个:继续展示你的努力你是什么意思?你只是展示了你的HTML,但没有任何努力来解决你自己的问题。@putvande你怎么知道如果我不努力,只是因为我没有粘贴我的代码?