在使用筛选器搜索类中的文本

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

search for text in classes using filter

问题

我有这个HTML:

<td class="catName" data-bs-toggle="collapse" data-bs-target=".category1">A</td>
<td class="catName" data-bs-toggle="collapse" data-bs-target=".category2">B</td>
<td class="catName" data-bs-toggle="collapse" data-bs-target=".category3">C</td>

我想使用 filter 函数来查找具有 innerText 为 "A" 的类。

尝试使用:

jQuery(".catName").filter(item => jQuery(item).text() == "A");

但这不起作用。

我正在使用 jQuery。我必须使用 filter。我必须使用箭头函数。

英文:

I have this html:

&lt;td class=&quot;catName&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;.category1&quot;&gt;A&lt;/td&gt;
&lt;td class=&quot;catName&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;.category2&quot;&gt;B&lt;/td&gt;
&lt;td class=&quot;catName&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;.category3&quot;&gt;C&lt;/td&gt;

I want to use the filter function to find the class that has the innerText "A".

Trying with:

jQuery(&quot;.catName&quot;).filter(item =&gt; jQuery(item).text() == &quot;A&quot;);

But it doesn't work.

I am using Jquery. I have to use filter. And I have to use an arrow function.

答案1

得分: 2

  1. 由于您的标记无效,什么都不会起作用。您需要添加一些相应的表格元素。

  2. filter 的第一个参数是 index,而第二个参数是 item(与原生JS数组方法的工作方式相反)。

在这个示例中,我正在过滤具有“A”的单元格并将其背景设置为红色。

英文:
  1. Due to your markup being invalid stands nothing will work. You need to add some corresponding table elements.

  2. The first argument of filter is index, and item is the second argument (which is the opposite way native JS array methods work).

In this example I'm filtering the cell with "A" and turning its background red.

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

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

$(&quot;.catName&quot;).filter((index, item) =&gt; {
  return $(item).text() == &quot;A&quot;;
}).css(&#39;background-color&#39;, &#39;red&#39;);

<!-- 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;tbody&gt;
    &lt;tr&gt;
      &lt;td class=&quot;catName&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;.category1&quot;&gt;A&lt;/td&gt;
      &lt;td class=&quot;catName&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;.category2&quot;&gt;B&lt;/td&gt;
      &lt;td class=&quot;catName&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;.category3&quot;&gt;C&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

答案2

得分: 2

// 在纯JavaScript中(不使用jQuery):

// 使用[.querySelectorAll()]来获取目标选择器元素的NodeList
// 使用[Array.from()]或使用数组的扩展运算符`...`将NodeList转换为数组
// 使用[Array.prototype.filter()]和箭头函数来迭代数组中的每个元素,并根据指定的布尔语句进行筛选(生成一个新数组)。
// 从这一点开始,您可以使用`Array.prototype.forEach()`来迭代您筛选后的元素数组:

const elsCatName = document.querySelectorAll(".catName");
const catName_A = [...elsCatName].filter(el => el.textContent === "A");

// 在示例中使用:
catName_A.forEach(el => el.style.color = "red");
<!-- language: lang-html -->
<table>
  <tbody>
    <tr>
      <td class="catName" data-bs-toggle="collapse" data-bs-target=".category1">A</td>
      <td class="catName" data-bs-toggle="collapse" data-bs-target=".category2">B</td>
      <td class="catName" data-bs-toggle="collapse" data-bs-target=".category3">C</td>
    </tr>
  </tbody>
</table>
// 对于完整性,使用jQuery可以表示为:

$(".catName").filter((i, el) => el.textContent === "A").css({color: "red"});
<!-- language: lang-html -->
<table>
  <tbody>
    <tr>
      <td class="catName" data-bs-toggle="collapse" data-bs-target=".category1">A</td>
      <td class="catName" data-bs-toggle="collapse" data-bs-target=".category2">B</td>
      <td class="catName" data-bs-toggle="collapse" data-bs-target=".category3">C</td>
    </tr>
  </tbody>
</table>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
// 或者,如果您需要一种懒惰匹配的方法(也可能匹配`"XXA"`)——可以使用jQuery的[:contains()选择器][5]

$(".catName:contains('A')").css({color: "red"});
<!-- language: lang-html -->
<table>
  <tbody>
    <tr>
      <td class="catName" data-bs-toggle="collapse" data-bs-target=".category1">A</td>
      <td class="catName" data-bs-toggle="collapse" data-bs-target=".category2">B</td>
      <td class="catName" data-bs-toggle="collapse" data-bs-target=".category3">C</td>
      <td class="catName" data-bs-toggle="collapse" data-bs-target=".category3">XXAZZ</td>
    </tr>
  </tbody>
</table>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
英文:

In pure JavaScript (without jQuery):

  • use .querySelectorAll() to get a NodeList of the targeted selector elements
  • use Array.from() or using the Array spread ... syntax convert the NodeList into an Array
  • use Array.prototype.filter() with Arrow Function to iterate every Element in your Array and filter it by a specified boolean statement (into a new Array). From that point on you can use Array.prototype.forEach() to iterate your filtered Array of Elements:

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

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

const elsCatName = document.querySelectorAll(&quot;.catName&quot;);
const catName_A = [...elsCatName].filter(el =&gt; el.textContent === &quot;A&quot;);

// Use in example like:
catName_A.forEach(el =&gt; el.style.color = &quot;red&quot;);

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

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td class=&quot;catName&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;.category1&quot;&gt;A&lt;/td&gt;
      &lt;td class=&quot;catName&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;.category2&quot;&gt;B&lt;/td&gt;
      &lt;td class=&quot;catName&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;.category3&quot;&gt;C&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

For completeness, in jQuery the above could be expressed like:

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

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

$(&quot;.catName&quot;).filter((i, el) =&gt; el.textContent === &quot;A&quot;).css({color: &quot;red&quot;});

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

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td class=&quot;catName&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;.category1&quot;&gt;A&lt;/td&gt;
      &lt;td class=&quot;catName&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;.category2&quot;&gt;B&lt;/td&gt;
      &lt;td class=&quot;catName&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;.category3&quot;&gt;C&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

or, if you need a lazy matching approach (might match &quot;XXA&quot; as well) — the jQuery's :contains() Selector

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

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

$(&quot;.catName:contains(&#39;A&#39;)&quot;).css({color: &quot;red&quot;});

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

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td class=&quot;catName&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;.category1&quot;&gt;A&lt;/td&gt;
      &lt;td class=&quot;catName&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;.category2&quot;&gt;B&lt;/td&gt;
      &lt;td class=&quot;catName&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;.category3&quot;&gt;C&lt;/td&gt;
      &lt;td class=&quot;catName&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;.category3&quot;&gt;XXAZZ&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

答案3

得分: 0

以下是翻译好的部分:

至少需要将&lt;td&gt;元素包装在&lt;table&gt;元素标签内,以使DOM理解其含义。

另一件需要更改的事情是,JQuery的filter()方法首先接收index,然后是JQuery的item作为第二个参数。所以你需要使用filter((i, item) =&gt;

$(&quot;.catName&quot;).filter((i, item) =&gt; $(item).text() == &quot;A&quot;);

查看完整示例,请参考JSFiddle:
https://jsfiddle.net/Lexdp7rt/

英文:

At a minimum &lt;td&gt; elements need to be wrapped inside &lt;table&gt; element tags for it to make sense to the DOM.

The other thing you need to change is that the JQuery filter() methods receives the index first, and the JQuery item as the second argument. So you need filter((i, item) =&gt;

$(&quot;.catName&quot;).filter((i, item) =&gt; $(item).text() == &quot;A&quot;);

See JSFiddle for full example:
https://jsfiddle.net/Lexdp7rt/

huangapple
  • 本文由 发表于 2023年6月25日 23:10:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551070.html
匿名

发表评论

匿名网友

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

确定