Javascript 如果找到文本内容,添加 for 循环以忽略列表中的各个节点
Javascript add in for loop to disregard various nodes in list if text content is found
我使用 querySelector 和 querySelectorAll 对我的 HTML 文档进行了查询,这会忽略所有 li ,这是代码:
var number2 = number;
var otherSubjRow = document.querySelector("#exploreTable");
if(otherSubjRow) {
var otherSubjCells = Row.querySelectorAll("li");
if (typeof(otherSubjCells) != 'undefined' && Cells != null)
{
l = otherSubjCells.length;
var otherSubjects = ""
var number2 = number;
for (var i = 0; i < l; i++) {
if (otherSubjCells[i].querySelectorAll("a").length < 1) {
otherSubjCells[i].innerText + " ");
otherSubjects += number2 + "." + otherSubjCells[i].innerText + " ";
number2++
}
}
}
这是我文档的一个片段:
<table id="exploreTable">
...
<tr>
<li>
<a></a>
</li>
</tr>
<tr><td>
<ul><li>International relations -- Handbook
<a></a>
</li></ul>
</td></tr>
<tr><td>
<ul><li>Titles by: Jose, Charles, editor.
<a></a>
</li></ul>
</td></tr>
<tr><td>
<ul><li>Series: Policy brief ; no. 2016-03
<a></a>
</li></ul>
</td></tr>
</table>
问题是我想忽略在我的 for 循环中捕获包含在文本 "Series: " 和 "Titles by: " 开头的所有文本。我正在尝试使用 || 将其添加到此处:
if (otherSubjCells[i].querySelectorAll("a").length < 1)
但不太确定这样做是否正确。或者在语句中添加另一个 if ?关于如何去做的任何想法?谢谢,干杯!
<table id="exploreTable">
<tr>
<li>
<a></a>
</li>
</tr>
<tr>
<td>
<ul>
<li>International relations -- Handbook
<a></a>
</li>
</ul>
</td>
</tr>
<tr>
<td>
<ul>
<li>Titles by: Jose, Charles, editor.
<a></a>
</li>
</ul>
</td>
</tr>
<tr>
<td>
<ul>
<li>Series: Policy brief ; no. 2016-03
<a></a>
</li>
</ul>
</td>
</tr>
</table>
<script>
const arr = document.getElementById("exploreTable").querySelectorAll('li');
const result = [].filter.call(arr,(el)=>{
return !(el.textContent.indexOf("Series:") === 0||el.textContent.indexOf("Titles by:") === 0)
});
const resText = result.map((el, index)=>{
return `${index+1} ${el.textContent}`;
});
console.log(result);
console.log(resText);
</script>
我使用 querySelector 和 querySelectorAll 对我的 HTML 文档进行了查询,这会忽略所有 li ,这是代码:
var number2 = number;
var otherSubjRow = document.querySelector("#exploreTable");
if(otherSubjRow) {
var otherSubjCells = Row.querySelectorAll("li");
if (typeof(otherSubjCells) != 'undefined' && Cells != null)
{
l = otherSubjCells.length;
var otherSubjects = ""
var number2 = number;
for (var i = 0; i < l; i++) {
if (otherSubjCells[i].querySelectorAll("a").length < 1) {
otherSubjCells[i].innerText + " ");
otherSubjects += number2 + "." + otherSubjCells[i].innerText + " ";
number2++
}
}
}
这是我文档的一个片段:
<table id="exploreTable">
...
<tr>
<li>
<a></a>
</li>
</tr>
<tr><td>
<ul><li>International relations -- Handbook
<a></a>
</li></ul>
</td></tr>
<tr><td>
<ul><li>Titles by: Jose, Charles, editor.
<a></a>
</li></ul>
</td></tr>
<tr><td>
<ul><li>Series: Policy brief ; no. 2016-03
<a></a>
</li></ul>
</td></tr>
</table>
问题是我想忽略在我的 for 循环中捕获包含在文本 "Series: " 和 "Titles by: " 开头的所有文本。我正在尝试使用 || 将其添加到此处:
if (otherSubjCells[i].querySelectorAll("a").length < 1)
但不太确定这样做是否正确。或者在语句中添加另一个 if ?关于如何去做的任何想法?谢谢,干杯!
<table id="exploreTable">
<tr>
<li>
<a></a>
</li>
</tr>
<tr>
<td>
<ul>
<li>International relations -- Handbook
<a></a>
</li>
</ul>
</td>
</tr>
<tr>
<td>
<ul>
<li>Titles by: Jose, Charles, editor.
<a></a>
</li>
</ul>
</td>
</tr>
<tr>
<td>
<ul>
<li>Series: Policy brief ; no. 2016-03
<a></a>
</li>
</ul>
</td>
</tr>
</table>
<script>
const arr = document.getElementById("exploreTable").querySelectorAll('li');
const result = [].filter.call(arr,(el)=>{
return !(el.textContent.indexOf("Series:") === 0||el.textContent.indexOf("Titles by:") === 0)
});
const resText = result.map((el, index)=>{
return `${index+1} ${el.textContent}`;
});
console.log(result);
console.log(resText);
</script>