简体   繁体   English

在 hover 上添加指向图像的链接

[英]add a link to a image on hover

EDIT: Wow thanks guys, you guys responded fast, this is my first time using this forum, Thanks a lot.编辑:哇,谢谢大家,你们回复很快,这是我第一次使用这个论坛,非常感谢。 The person who I marked as an answer knew was what I was talking about, to the others also thank for responding我标记为答案的人知道我在说什么,其他人也感谢回复

I was trying to add a link to an image when you hover over it.当您通过 hover 覆盖图像时,我试图添加指向图像的链接。 when you put your cursor over it it changes images and that image has a link.当您将 cursor 放在它上面时,它会更改图像并且该图像具有链接。 can anyone help me?谁能帮我?

HTML HTML

<img src="don.png" class="artist" onmouseover="hover(this);" onmouseout="unhover(this);" a href="https://privatelink.com"/>

JS JS

function hover(element) {
  element.setAttribute('src', 'ht.png');
}

function unhover(element) {
  element.setAttribute('src', 'don.png');
}

First of all you have an error in your html syntax.首先,您的 html 语法有错误。 a and href are not attributes of an img element tag. ahref不是img元素标签的属性。 Check it here for available attributes.在此处查看可用属性。 But if you would like to store your custom data with an image, you can add data-* attribute.但是,如果您想将自定义数据与图像一起存储,则可以添加data-*属性。 Check it here .在这里检查。 You can then use it in JS if you need it.如果需要,您可以在 JS 中使用它。

From the all solutions at Add link to image dynamically you can use:动态添加链接到图像的所有解决方案中,您可以使用:

var parentEl = document.getElementById("myimg").parentElement;
var imgEl = parentEl.innerHtml;
parentEl.innerHtml = '<a href="test.html">' + imgEl + '</a>';

... or use jQuery .wrap() also mentioned on the link above. ...或使用 jQuery .wrap()也在上面的链接中提到。

Here is JSFiddle example for your case.这是您的案例的JSFiddle示例。

you can do like this你可以这样做

HTML HTML

<img src="don.png" class="artist" id="myImage" onmouseover="hover("myImage")" onmouseout="unhover("myImage")" a href="https://privatelink.com"/>

JS JS

 function hover(element) {
     document.getElementById(element).src='ht.png'
  }

 function unhover(element) {
     document.getElementById(element).src='don.png'
  }

 $(document).ready(function() { $("#myimage").hover(function() { var src = "https://privatelink.com"; var a = $("<a/>").attr("href", src); $( this ).wrap(a); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <figure id="myimage"> <img src="pic_trulli.jpg" alt="Trulli" style="width:100%"> <figcaption> Fig.1 - Trulli, Puglia, Italy. </figcaption> </figure>

 const linkedImg = document.querySelector("#image_link"); linkedImg.addEventListener("mouseover", (evt) => toggleImage(evt, "over")); linkedImg.addEventListener("mouseout", (evt) => toggleImage(evt, "out")); function toggleImage(evt, id) { if (id === "over") { linkedImg.setAttribute("href", "someurl1.html"); // change image linkedImg.querySelector("img").setAttribute( "src", "https://images.unsplash.com/photo-1542353436-312f0e1f67ff?auto=format&fit=crop&w=400&q=80" ); } else { linkedImg.setAttribute("href", "someurl2.html"); // change image linkedImg.querySelector("img").setAttribute( "src", "https://images.unsplash.com/photo-1558981852-426c6c22a060?fit=crop&w=500&q=80" ); } }
 <a href="#" id="image_link"> <img src="https://images.unsplash.com/photo-1558981852-426c6c22a060?fit=crop&w=500&q=80" class="artist"/> </a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM