Set all destination links for the page

I have links that are dynamically generated, and I need to set a goal for all of them. How can I do this using javaScript. I found something similar to jQuery working ..

$("a").attr('target', '_top');

but I don’t want to use the library for this, and I guess a couple lines of javaScript will take care of this ... I just don’t know how to write this.

To clarify what I'm doing, the links are generated using javaScript and the recommendation engine, ATG, and I call this in an iframe, which I need to break to the top. I assume that I just need a way to define all the links in the DOM, it does not have to be bound to a specific identifier. Is it possible to associate a dynamically generated attribute with a dynamically generated link? There is the possibility of creating custom rendering, but I hope to avoid this route.

+3
source share
3 answers

Something like that

var anchorElements = document.getElementsByTagName("a");

for (var i = 0; i < anchorElements.length; i ++)
{
  anchorElements[i].setAttribute("target", "_top");
}
+3
source

jQuery, , . , , JavaScript jQuery, , ...

var anchors = document.getElementById('myDiv').getElementsByTagName('a');

for (var i = 0; i < anchors.length; i++) {
    anchors[i].setAttribute('target', '_top');
}

, jQuery - , . id, getElementById().

+2
+1

Source: https://habr.com/ru/post/1745392/


All Articles