在jQuery中按索引添加自定义类。

huangapple go评论70阅读模式
英文:

adding custom class by index in jQuery

问题

以下是您要翻译的部分:

"As someone new to JS and jQuery, I'm working on a table with multiple columns that contain numbers, except for the last one which has text. My goal is to first add the 'custom-sortable' class to all elements in the last column that equal the word 'Camp', and then add the 'custom-sortable' class to all elements in the entire table where the last column equals to 'Camp'. I've tried different approaches, but haven't had success. My latest idea is to use the index of elements in the last column that include the word 'Camp'. Any help or hints would be appreciated.

Edit: The table is a html, the class is just to change the background color of all elements equal to 'Camp'.

The following code works in terms of adding the class to the last column.

.custom-sortable .rank-list-item.rank-list-item-Camp {
    background-color: grey;
}

$(document).ready(function() {
    var targetIndex = -1;
    $('.custom-sortable .rank-list-item').each(function(index) {
        if ($(this).text() === 'Camp') {
            targetIndex = index;
            $(this).addClass('custom-sortable rank-list-item-Camp');
        }
    });
    if (targetIndex >= 0) {
        $('.custom-sortable .rank-list-item').eq(targetIndex).addClass('custom-sortable');
    }
});

But i wish to end up with these results"

英文:

As someone new to JS and jQuery, I'm working on a table with multiple columns that contain numbers, except for the last one which has text. My goal is to first add the "custom-sortable" class to all elements in the last column that equal the word "Camp", and then add the "custom-sortable" class to all elements in the entire table where the last column equals to "Camp". I've tried different approaches, but haven't had success. My latest idea is to use the index of elements in the last column that include the word "Camp". Any help or hints would be appreciated.

Edit: The table is a html, the class is just to change the background color of all elements equal to "Camp".

The following code works in terms of adding the class to the last column.

.custom-sortable .rank-list-item.rank-list-item-Camp {
    background-color: grey;
}

$(document).ready(function() {
            var targetIndex = -1;
            $('.custom-sortable .rank-list-item').each(function(index) {
            if ($(this).text() === 'Camp') {
            targetIndex = index;
            $(this).addClass('custom-sortable rank-list-item-Camp');
        }
        });
        if (targetIndex >= 0) {
        $('.custom-sortable .rank-list-item').eq(targetIndex).addClass('custom-sortable');
        }
        });

在jQuery中按索引添加自定义类。

But i wish to end up with these results
在jQuery中按索引添加自定义类。

答案1

得分: 2

当您定位到目标时,可以使用.closest('tr')来遍历DOM到父tr。然后,您可以使用.find('td')来定位所有子td。以下是一个简化的代码片段,用于演示,但您只需要插入:.closest('tr').find('td')

$(document).ready(function() {
  $('td').each(function(index) {
    if ($(this).text() === 'Camp') {
      $(this).closest('tr').find('td').addClass('rank-list-item-Camp');
    }
  });
});
.rank-list-item-Camp {
  background-color: grey;
}

table {
  border-collapse: collapse;
}

td {
  padding: 1em;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td>0</td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
  </tr>
  <tr>
    <td>0</td>
    <td>1</td>
    <td>Camp</td>
    <td>3</td>
  </tr>
  <tr>
    <td>0</td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
  </tr>
</table>

注:如果您刚开始学习,我建议学习纯JavaScript(ES6+)。jQuery正在变得过时,而且不再具备优势。

英文:

When you locate your target you can traverse up the DOM to the parent tr with .closest(&#39;tr&#39;). You can then target all the child tds with .find(&#39;td&#39;). Here is an abbreviated snippet to demonstrate, but you only need to insert: .closest(&#39;tr&#39;).find(&#39;td&#39;)

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).ready(function() {

  $(&#39;td&#39;).each(function(index) {

    if ($(this).text() === &#39;Camp&#39;) {

      $(this).closest(&#39;tr&#39;).find(&#39;td&#39;).addClass(&#39;rank-list-item-Camp&#39;);

    }

  });

});

<!-- language: lang-css -->

.rank-list-item-Camp {
  background-color: grey;
}

table {
  border-collapse: collapse;
}

td {
  padding: 1em;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;table&gt;
  &lt;tr&gt;
    &lt;td&gt;0&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;2&lt;/td&gt;
    &lt;td&gt;3&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;0&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;Camp&lt;/td&gt;
    &lt;td&gt;3&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;0&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;2&lt;/td&gt;
    &lt;td&gt;3&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

<!-- end snippet -->

P.S. If you are just starting to learn, then I would recommend just learning vanilla JavaScript (ES6+). jQuery is becoming obsolete and adds more weight than advantage anymore.

答案2

得分: 1

It would make more sense to target the parent, instead of the child.

$(document).ready(function() {
  $('.custom-sortable').each(function(index) {
    let isCamp = false;

    $(this).find('.rank-list-item').each(function() {
      if ($(this).text() === 'Camp') {
        isCamp = true;
        $(this).addClass('custom-sortable rank-list-item-Camp');
      }
    });

    if (isCamp) {
      $(this).addClass('custom-sortable');
    }
  });
});

This way you will find all of the rows that have "Camp" as the last cell, and you don't need to keep track of the index in a separate variable (that will be overwritten.) You also don't need to traverse back up the parent after finding the child.

英文:

It would make more sense to target the parent, instead of the child.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).ready(function() {
  $(&#39;.custom-sortable&#39;).each(function(index) {
    let isCamp = false;

    $(this).find(&#39;.rank-list-item&#39;).each(function() {
      if ($(this).text() === &#39;Camp&#39;) {
        isCamp = true;
        $(this).addClass(&#39;custom-sortable rank-list-item-Camp&#39;);
      }
    });

    if (isCamp) {
      $(this).addClass(&#39;custom-sortable&#39;);
    }
  });
});

<!-- end snippet -->

This way you will find all of the rows that have "Camp" as the last cell, and you don't need to keep track of the index in a separate variable (that will be overwritten.) You also don't need to traverse back up the parent after finding the child.

huangapple
  • 本文由 发表于 2023年3月12日 06:46:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定