gpt4 book ai didi

javascript - 获取与javascript中特定路径匹配的所有href

转载 作者:行者123 更新时间:2023-11-28 19:10:01 26 4
gpt4 key购买 nike

我需要从网页中获取所有 href 并将它们附加为可点击的链接,但我不知道如何获取它们。我正在尝试获取遵循此格式的 href

<table class="imageList">
<tbody>
<tr>
<td> <a href="build/artifacts/image1.png">One</a> </td>
</tr>
<tr>
<td> <a href="build/artifacts/image2.png">Two</a> </td>
</tr>
<tr>
<td> <a href="build/artifacts/image3.png">Three</a> </td>
</tr>
</tbody>
</table>

等等...

所有图像都有 href 前缀 build/artifacts/并且位于表“imageList”内,但如何获取所有图像(href)。我只对 href 感​​兴趣,而不是整个表格或其他可能存在的非元素。

最佳答案

使用CSS Attribute Selectors :

'table.imageList > tbody > tr > td > a[href^="build/artifacts/"]'

然后使用 querySelectorAll :

anchors = document.querySelectorAll('table.imageList > tbody > tr > td > a[href^="build/artifacts/"]');

最后,您可能 iterate over the result set and extract the hrefs :

hrefs = Array.prototype.map.call(anchors, function(item) { return item.getAttribute('href'); } );

[Array.prototype.map 的制作者为 Vohuman;我纳入了他的想法以提供完整的解决方案]

关于javascript - 获取与javascript中特定路径匹配的所有href,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30878455/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com