javascript - Most efficient way to change class of the anchor element based on query string -
criteria - prefer not use additional library, want perform pure javascript, happy use 1 if needed.
i want add additional class anchor elements have query string of 'xyz=negate'. page typically has more 200 anchor elements. looking efficient way achieve this.
the user base website still has decent number of ie 8 (intranet site) , hence looking efficient code. page has anchor elements following
<a href="http://www.w3schools.com?id=43&xyz=negate">visit w3schools.com!</a> <a href="http://www.w3schools.com?id=47&xyz=nonnegate">visit w3schools.com!</a> <a href="http://www.w3schools.com?id=3&xy=negate">visit w3schools.com!</a> <a href="http://www.w3schools.com?id=42&xyz=negate&yz=external">visit w3schools.com!</a> <a href="http://www.w3schools.com?id=43&xyz=negate">visit w3schools.com!</a>
i want add class first, fourth , fifth anchor element in above example.
use *
in selector match, example:
var links=document.queryselectorall("a[href*='xyz=negate']");
it find links href containing xyz=negate in part of attribute.
in pure js not possible set class collection in jquery, must go through every element , set class.
for ( var in links ){ links[i].classlist.add("someclass"); }
for ie8 ( classlist not exists ):
for ( var in links ){ links[i].classname+="someclass"; }
Comments
Post a Comment