[英]Add a link to an image in a Javascrip
hello everybody!大家好!
First of all I have to tell you I'm a newbie in terms of HTML, Javascript and coding in general.首先,我必须告诉您,就 HTML、Javascript 和一般编码而言,我是新手。 But I spent last weeks to learn as much as I can.
但我花了几周时间尽可能多地学习。 Now, I run a website and my dev team is not workgin with me anymore, then I have to do all by myself.
现在,我经营一个网站,我的开发团队不再和我一起工作,然后我必须自己做所有事情。
My problem is: I have a sort of "big banner" and the image shown is automatically picked up based on your browser resolution (that's fine, I asked for this).我的问题是:我有一种“大横幅”,显示的图像是根据您的浏览器分辨率自动拾取的(没关系,我要求这个)。 Now, what I'm trying to do is to add a hyperlink to this image.
现在,我要做的是为这张图片添加一个超链接。 I tried many thing, but I couldnt.
我尝试了很多东西,但我做不到。
Please could you tell me how to edit this code?请您告诉我如何编辑此代码?
Thank you谢谢
Miki三木
<div class="main top">
<div class="main-holder">
<div class="cover-holder big-img-holder" id="img-holder">
<script>
function randomInteger(min, max) {
var rand = min + Math.random() * (max - min)
rand = Math.round(rand);
return rand;
}
var rand = randomInteger(1, {{$lang.extras.number_screenshots_rotations}});
var picture = document.createElement('picture');
var source = document.createElement('source');
source.srcset = '{{$config.statics_url}}/images/bg_rotations/extras-' + rand + 'bd_HD.jpg';
source.setAttribute('media', '(max-width: 2000px)');
var img = document.createElement('img');
img.src = '{{$config.statics_url}}/images/bg_rotations/extras-' + rand + 'bd.jpg';
picture.appendChild(source);
picture.appendChild(img);
document.getElementById('img-holder').appendChild(picture);
</script>
</div>
Usually you can set a hyper link with the HTML tag <a>
通常您可以使用 HTML 标签
<a>
设置超链接
So you could try所以你可以试试
<a href="your-URL">
<div class="cover-holder big-img-holder" id="img-holder">
<script>
...
</script>
</div>
</a>
Wrap the script in something like将脚本包装在类似的东西中
<a id="img-link">
or just change img-holder to be an <a>
instead of a <div>
或者只是将 img-holder 更改为
<a>
而不是<div>
Then somewhere in your script set the url for that <a>
container然后在您的脚本中的某处为该
<a>
容器设置 url
document.getElementById("img-link").href="url";
There's no href attribute to an image itself, so you must provide it to <a>
图像本身没有 href 属性,因此您必须将其提供给
<a>
That is if you wanna do it dynamically, if the url is always the same, just wrap your <script>
block with this也就是说,如果您想动态执行此操作,如果 url 始终相同,只需将您的
<script>
块包装为
<a href="#">
<img>
</a>
It is easier when you know that DOM is gonna have the same structure, in this case, an image, and an <a>
tag wrapping it, just set ids for both, and manipulate their attributes, otherwise, you can also create this <a>
element in Javascript, and set the image with .setChild()
to it当您知道 DOM 将具有相同的结构时会更容易,在这种情况下,一个图像和一个包装它的
<a>
标签,只需为两者设置 id,并操作它们的属性,否则,您也可以创建这个<a>
Javascript 中的元素,并使用.setChild()
为其设置图像
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.