最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I select all links on page that contain a certain word? - Stack Overflow

programmeradmin1浏览0评论

How do I select all links on a page that contain a certain keyword. I want all links that contain 'amazon' in the link. Some links might be https, or http so I can't do it like so

let amazonLinks = document.querySelectorAll('[href*=""]');

How do I select all links on a page that contain a certain keyword. I want all links that contain 'amazon' in the link. Some links might be https, or http so I can't do it like so

let amazonLinks = document.querySelectorAll('[href*="https://www.amazon"]');
Share Improve this question asked Aug 4, 2019 at 18:43 Andre MacNamaraAndre MacNamara 191 silver badge7 bronze badges 1
  • 1 const amazonLinks = document.querySelectorAll('a[href*="amazon"]') – emcee22 Commented Aug 4, 2019 at 18:49
Add a ment  | 

3 Answers 3

Reset to default 5

you can pick all the links in the page using the below selector:

document.querySelectorAll('a[href*="amazon"]').forEach(function(a){
console.log(a.href)});

Cheers! hope this helps...

Here are the wildcards for the querySelector:

[attr^='someValue'] will match all ids starting with someId.

[attr$='someValue'] will match all ids ending with someId.

[attr*='someValue'] will match all ids containing someId.

If you need either http or https you should query as ://amazon.

Using jQuery :

$('a[href*="/YOUR-TEXT-HERE/"]').attr('href')
发布评论

评论列表(0)

  1. 暂无评论