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

admin2025-04-20  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')
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745123634a286296.html

最新回复(0)