繁体   English   中英

使用 javascript 从列表中获取特定 url

[英]Get a specific url from a list with javascript

<p class="URL-List">
    Download : 
    <a href="https://exampleAA.com"><strong>AA</strong></a> / 
    <a href="https://exampleBB.com"><strong>BB</strong></a> / 
    <a href="https://exampleCC.com"><strong>CC</strong></a>
</p>

我的代码

AA = URL[0].getElementsByTagName("a")
AA[0]

我会得到第一个 URL:

https://exampleAA.com

但是名单变了

<p class="URL-List">
    Download : 
    <a href="https://exampleCC.com"><strong>CC</strong></a> / 
    <a href="https://exampleDD.com"><strong>DD</strong></a> / 
    <a href="https://exampleSS.com"><strong>SS</strong></a> /
    <a href="https://exampleAA.com"><strong>AA</strong></a> / 
    <a href="https://exampleFF.com"><strong>FF</strong></a> / 
    <a href="https://exampleBB.com"><strong>BB</strong></a>
</p>

我的代码

AA = URL[0].getElementsByTagName("a")
AA[0]

现在我得到:`

https://exampleCC.com

`但我想要

https://exampleAA.com

我怎样才能找到那个特定的 URL?

如果你知道你想要哪个链接,你可以使用.querySelector()属性选择器语法

 let myLink = document.querySelector("a[href='https://exampleAA.com']"); console.log(myLink.textContent, myLink.href);
 <p class="URL-List"> Download : <a href="https://exampleCC.com"><strong>CC</strong></a> / <a href="https://exampleDD.com"><strong>DD</strong></a> / <a href="https://exampleSS.com"><strong>SS</strong></a> / <a href="https://exampleAA.com"><strong>AA</strong></a> / <a href="https://exampleFF.com"><strong>FF</strong></a> / <a href="https://exampleBB.com"><strong>BB</strong></a> </p>

不要在 2020 年使用.getElementsByTagName()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM