Is there any way to href the A tag that is not specified by ID, class, etc. in JavaScript?

Asked 1 years ago, Updated 1 years ago, 32 views

As I am a beginner in JavaScript, I am sorry if this is a basic question.
I am currently creating a macro that automates the operation of an existing website.
I am currently working on creating automation macros using JavaScript.
We are faced with situations where we need to specify an A tag that does not have a Class or ID and transition to a link beyond it.
Is there any good way?

javascript

2022-09-30 20:17

2 Answers

I think using jQuery is the easiest way.
It can be retrieved using a selector.
I think it will probably be under some control, so I guess it's like the following.
If you don't know the ID of the parent element, I think it's better to nest it by applying the following.

$("#ID of parent element").find("a").each(function(){
    alert($(this).attr("href"));
});

■For JavaScript only

// If you want to retrieve the contents of tag A anywhere
tags = document.getElementsByTagName('a');
for (vari=0;i<tags.length;i++){
    alert(tags[i].getAttribute("href"))
}


// To retrieve the A tag under the parent ID
varoya= document.getElementById
tags =oya.getElementsByTagName('a');
for (vari=0;i<tags.length;i++){
    alert(tags[i].getAttribute("href"))
}


2022-09-30 20:17

@ It seems that it has been resolved by Mr. SSDev, so please refer to the other writing method on time.

If a modern browser is a prerequisite, it is easy to write without jQuery.

Ex:Exclude links only starting with https://google

const links= document.querySelectorAll('a[href^="https://google"]');
for (const link of links) {
  console.log(link);
}


2022-09-30 20:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.