编辑:我忘了搜索栏并添加了它。
你好,
我目前正在研究一种方法来过滤一个网站的文本。有div,在div内部是p元素,它包含文本和客栈标记,它们与搜索相关。我的想法是在div上循环,然后对每个div循环通过p标记,然后循环在inn标记上,然后只有当有一个包含匹配的inn标记的p标记时,才显示div。我不想要一个插件解决方案,如果可能的话,用js或jquery,因为我只使用这两种语言。
简而言之,我想隐藏所有有0匹配的div,也要隐藏0匹配的所有p标记,并且我希望将过滤器与客栈标记的innerText进行比较。
我一直在查找w3schools搜索过滤器选项,但我觉得我被困住了,我希望在如何处理这个问题上提供一些帮助或其他方法的投入。另一个问题是,如果我搜索图1,我也会得到图10或图12的匹配。有方法只显示精确的匹配吗?
<div class="search-box">
<input class="search-input" id="myInputDesc" placeholder="Start Looking For Something!"
type="text" />
<a class="search-btn" onclick="searchMeDesc()">
<svg height="30px" viewbox="0 0 30 30" width="30px" xmlns=" http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="surface509077">
<path
d="M 13 3 C 7.488281 3 3 7.488281 3 13 C 3 18.511719 7.488281 23 13 23 C 15.398438 23 17.597656 22.148438 19.324219 20.734375 L 25.292969 26.707031 C 25.542969 26.96875 25.917969 27.074219 26.265625 26.980469 C 26.617188 26.890625 26.890625 26.617188 26.980469 26.265625 C 27.074219 25.917969 26.96875 25.542969 26.707031 25.292969 L 20.734375 19.320312 C 22.148438 17.597656 23 15.398438 23 13 C 23 7.488281 18.511719 3 13 3 Z M 13 5 C 17.429688 5 21 8.570312 21 13 C 21 17.429688 17.429688 21 13 21 C 8.570312 21 5 17.429688 5 13 C 5 8.570312 8.570312 5 13 5 Z M 13 5 "
style=" stroke:none;fill-rule:nonzero;fill:rgb(100%,100%,100%);fill-opacity:1;">
</path>
</g>
</svg>
</a>
</div>
<div>
<p>
considerations for viable autonomous vehicle systems. The first is a standardization
of safety assurance, including requirements that every self-driving car must satisfy
to ensure safety, and how those requirements can be verified. The second is
scalability, as engineering solutions that lead to unleashed costs will not scale to
millions of cars and may prevent widespread or even not so widespread adoption of
autonomous vehicles. Thus, there is a need for an interpretable, mathematical model
for safety assurance and a design of a system that adheres to safety assurance
requirements while being scalable to millions of cars.
<inn>Fig. 1</inn>
considerations for viable autonomous vehicle systems. The first is a standardization
of safety assurance, including requirements that every self-driving car must satisfy
to ensure safety, and how those requirements can be verified. The second is
scalability, as engineering solutions that lead to unleashed costs will not scale to
millions of cars and may prevent widespread or even not so widespread adoption of
autonomous vehicles. Thus, there is a need for an interpretable, mathematical model
for safety assurance and a design of a system that adheres to safety assurance
requirements while being scalable to millions of cars.
<inn>Fig. 12</inn>
</p>
<p> The present disclosure relates generally to autonomous vehicle navigation. Additionally, this disclosure relates to systems and methods for navigating according to potential accident liability constraints.</p>
<p> considerations for viable autonomous vehicle systems. The first is a standardization
of safety assurance, including requirements that every self-driving car must satisfy
to ensure safety, and how those requirements can be verified. The second is
scalability, as engineering solutions that lead to unleashed costs will not scale to
millions of cars and may prevent widespread or even not so widespread adoption of
autonomous vehicles. Thus, there is a need for an interpretable, mathematical model
for safety assurance and a design of a system that adheres to safety assurance
requirements while being scalable to millions of cars.
</p>
</div>
<div>
<p>
considerations for viable autonomous vehicle systems. The first is a standardization
of safety assurance, including requirements that every self-driving car must satisfy
to ensure safety, and how those requirements can be verified. The second is
scalability, as engineering
<inn>Fig. 10</inn>
solutions that lead to unleashed costs will not scale to
millions of cars and may prevent widespread or even not so widespread adoption of
autonomous vehicles. Thus, there is a need for an interpretable, mathematical model
for safety assurance and a design of a system that adheres to safety assurance
requirements while being scalable to millions of cars.
</p>
<p> considerations for viable autonomous vehicle systems. The first is a standardization
of safety assurance, including requirements that every self-driving car must satisfy
to ensure safety, and how those requirements can be verified. The second is
scalability, as engineering solutions that lead to unleashed costs will not scale to
millions of cars and may prevent widespread or even not so widespread adoption of
autonomous vehicles. Thus, there is a need for an interpretable, mathematical model
for safety assurance and a design of a system that adheres to safety assurance
requirements while being scalable to millions of cars.
</p>
</div>JS代码
//declare all variables needed
var input, filter, figref, test;
input = document.getElementById('myInputDesc');
filter = input.value.toUpperCase().trim();
figref = document.getElementsByTagName('figref');
test = document.getElementsByTagName('div')
for (j = 0; j < test.length; j++) {
p = test[j].getElementsByTagName("p");
for (let i = 0; i < p.length; i++) {
fig = p[i].getElementsByTagName("inn");
if (fig.length == 0) {
p[i].style.display = "none";
} else {
for (let f = 0; f < fig.length; f++) {
ptxt = fig[f].innerText;
console.log(ptxt)
if (ptxt.toUpperCase() == filter) {
console.log(p[i])
p[i].style.display = "none";
} else {
console.log(test[j])
p[i].style.display = "block";
}
}
}
}
} ```发布于 2022-06-17 12:09:45
因此,我认为最有效的方法是搜索<inn>元素,然后选择最近的div祖先。类似于:
const input = document.getElementById('myInputDesc');
const divs = input.querySelectorAll('inn').each(el => el.closest('div'))closest的文档:https://developer.mozilla.org/en-US/docs/Web/API/Element/closest
对p元素作必要的修改。然后,您可以将一个类应用到每个有匹配项的div/p中,并隐藏其他类。
如果您需要对旧浏览器的支持,请参见:Find the closest ancestor element that has a specific class
https://stackoverflow.com/questions/72659078
复制相似问题