javascript - Change all href inside a DIV with class -
I am trying to replace all the href links within a curtain DIV which uses the class. So far, I can change all the links in one page using this snippet:
function sl () {var l = document.getElementsByTagName ('a'); For {i = 0; i & lt; l.length; i ++} {var cl = l [i] .href; L [i] .href = cl.replace (cl, 'http: // domain, com /' + cl);}} window.onload = Sl;
But this is not an ideal solution.
I have tried to use this line:
var l = getElementsByClassName ('class') .getElementsByTagName ('a');
But that does not work.
Note: No Jquery solution, javascript only thanks, I just do not want to load jquery for a small snippet.
is a well supported method (even IE8 supports it) , So that all of you one
elements:
var l = document.querySelectorAll ('Div.class a'); I tried to use this line: var l = getElementsByClassName ('class'). GetElementsByTagName ('a');
but that does not work
This is because, like getElementsByTagName
, getElementsByClassName
List of Elements If you know that there is only one element with that class, then you can reference it directly:
var l = getElementsByClassName ( 'Class') [0] .getElementsByTagName ('a');
Otherwise you should get a for loop with .class
elements and get the loop call getElementsByTagName ('A')
a
element for
Comments
Post a Comment