I have the following little snippet of code at the bottom of my page before the closing body tag:
var myAnchor = document.getElementById("tools").getElementsByTagName("a")[0];
var myHref = myAnchor.href;
myHref = "http://www.failblog.org";
alert(myHref);
The page alerts "http://www.failblog.org" as expected, but it doesn't actually change the value of the href attribute for the anchor. The link stubbornly retains its original href. Can anyone tell me what I'm doing wrong?
You did it the wrong way , try this:
myHref = "http://www.failblog.org";
var myAnchor = document.getElementById("tools").getElementsByTagName("a")[0];
myAnchor.href = myHref;
alert(myHref);
When setting var myHref = myAnchor.href;
, the string value of myHref
will be set to the string value of myAnchor.href;
, the variable isn't assigned as a reference, but as a copy of the value.
Change the code to this:
var myHref = "http://www.failblog.org";
var myAnchor = document.getElementById("tools").getElementsByTagName("a")[0];
myAnchor.href = myHref;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.