简体   繁体   English

href javascript在我的图片上不起作用

[英]href javascript is not working on my image

I'm adding images stored in firebase to my html page from the database. 我正在将存储在Firebase中的图像从数据库添加到我的html页面。 That works and the image shows up, but I am unable to make the href property work on the img element. 那行得通,并且显示了图像,但是我无法使href属性在img元素上起作用。 I want it to be clickable. 我希望它是可点击的。 Suggestions what I am doing wrong here? 建议我在这里做错了什么?

var img = document.createElement("img");
img.src = snapshot.child("urlToImage " + (i) + " Content").val();
img.href = snapshot.child("urlToImage " + (i) + " Content").val();
img.height = 30;
img.width = 30;
contentA[i].appendChild(img);

Clickable image 可点击的图片

 function clickMe(){ alert('Clicked!'); } 
 <!DOCTYPE html> <html> <body> An image as a link: <a onclick="clickMe()"> <img src="https://s3.eu-central-1.amazonaws.com/live-resource/productCategories/HF-AT/HF_PRINT_WEB_F781A5E7-1575-4D81-8317-BC51C8653EBB.jpeg" border="0" alt="W3Schools" src="logo_w3s.gif" width="100" height="100"> </a> </body> </html> 

You have to wrap the img element in an a tag. 您必须将img元素包装在a标签中。

<a href='urlToImage'><img src='urlToImage' /></a>

You can of course have an href attribute on an img if you want to, it just doesn't have any special default meaning to browsers like it would on an anchor 当然,如果需要,您可以在img上具有href属性,它对浏览器没有任何特殊的默认含义,就像锚点一样

Is href a valid attribute of img? href是img的有效属性吗? I've never seen that before. 我以前从未见过。 If you want it to be clickable just wrap it in a anchor. 如果要使其可单击,只需将其包裹在锚点中即可。

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

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