英文:
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:
<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>
I want to use the filter
function to find the class that has the innerText "A".
Trying with:
jQuery(".catName").filter(item => jQuery(item).text() == "A");
But it doesn't work.
I am using Jquery
. I have to use filter
. And I have to use an arrow function
.
答案1
得分: 2
-
由于您的标记无效,什么都不会起作用。您需要添加一些相应的表格元素。
-
filter
的第一个参数是index
,而第二个参数是item
(与原生JS数组方法的工作方式相反)。
在这个示例中,我正在过滤具有“A”的单元格并将其背景设置为红色。
英文:
-
Due to your markup being invalid stands nothing will work. You need to add some corresponding table elements.
-
The first argument of
filter
isindex
, anditem
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 -->
$(".catName").filter((index, item) => {
return $(item).text() == "A";
}).css('background-color', 'red');
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<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>
<!-- 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(".catName");
const catName_A = [...elsCatName].filter(el => el.textContent === "A");
// Use in example like:
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>
<!-- end snippet -->
For completeness, in jQuery the above could be expressed like:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(".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>
<!-- end snippet -->
or, if you need a lazy matching approach (might match "XXA"
as well) — the jQuery's :contains()
Selector
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(".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>
<!-- end snippet -->
答案3
得分: 0
以下是翻译好的部分:
至少需要将<td>
元素包装在<table>
元素标签内,以使DOM理解其含义。
另一件需要更改的事情是,JQuery的filter()
方法首先接收index
,然后是JQuery的item
作为第二个参数。所以你需要使用filter((i, item) =>
$(".catName").filter((i, item) => $(item).text() == "A");
查看完整示例,请参考JSFiddle:
https://jsfiddle.net/Lexdp7rt/
英文:
At a minimum <td>
elements need to be wrapped inside <table>
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) =>
$(".catName").filter((i, item) => $(item).text() == "A");
See JSFiddle for full example:
https://jsfiddle.net/Lexdp7rt/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论