打印图像的链接

Link for printing image

本文关键字:链接 图像 打印      更新时间:2023-09-26

实际上我需要一点Java Script,但我绝对不擅长它。所以我真的需要你的帮助。

我的问题是,我需要一个脚本,它将我从id到变量的url保存为变量,并输出变量如下:<a href="+ variable +">LINK</a>

请注意,我想从其中获得链接的ID位于页面的某个地方,而脚本可能位于底部。

我的意思的例子:

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

非常感谢您的帮助!

首先,id="http://www.example.com"可能看起来很奇怪,但根据HTML 5规范,它应该是OK的。在DIV元素中使用NAME属性也是不常见的。

第二,为你的链接指定另一个id会更容易,也许像<a id="link_id">LINK</a>

然后,你可以在你的脚本块

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; 
}