Link for printing image

2019-09-11 10:52发布

Actually I need a bit of Java Script, but I am definitely not good at it. So I would really need your help.

My problem is, that I would need a script, which saves me the url from an id to a variable and outputs the variable something like this: <a href="+ variable +">LINK</a>.

Note, that the ID, where I would like to get the link from, is somewhere in the page and the script likely at the bottom.

EXAMPLE OF WHAT I MEAN:

<div id="http://www.example.com" name="url">
  <p>Some content</p>
</div>
.
.
.
.
<a href="+ variable +">LINK</a>
.
.
.
<script>
   variable = getIdWhereNameIsURL;
   ....
</script>

Would really appreciate your help!

1条回答
Melony?
2楼-- · 2019-09-11 11:42

First of all, id="http://www.example.com" may seems odd, but it should be OK per the HTML 5 specs. It is also unusual to have a NAME attribute for a DIV element.

Second, it will be easier to specify another id for your LINK, perhaps something like <a id="link_id">LINK</a>.

Then, you can have the below in your script block

var theDIV = document.getElementsByTagName("div")[0];
// here assuming it is the first DIV, otherwise, loop through the elements
// document.getElementsByName("url")[0].id may not work
if (theDIV.name == "url") {
  var theLink = document.getElementById("link_id");
  theLink.href = theDIV.id; 
}
查看更多
登录 后发表回答