Html 链接一个 img id="logo"

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17085053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 09:23:22  来源:igfitidea点击:

Link a img id="logo"

htmlcss

提问by user2465543

I have - img id="logo" SO I can have my logo on my website, the full code for it being:

我有 - img id="logo" 所以我可以在我的网站上有我的标志,它的完整代码是:

<img id="logo" src="nsc/logo2.png" alt="logo" />

How can I link this logo image using <a href="">???

如何使用<a href="">???链接此徽标图像

I've tried many different ways but they don't work :/

我尝试了很多不同的方法,但它们不起作用:/

回答by Antony

<a href="www.example.com"><img id="logo" src="nsc/logo2.png" alt="logo" /></a>

回答by Ravi Gadag

It would be nice to embed in a link tag. :)

嵌入链接标签会很好。:)

<a href="your.html">
    <img id="logo" src="nsc/logo2.png" alt="BrandLog" />
</a>

it's clean and no need of javascript :)

它很干净,不需要 javascript :)

回答by Kyle

It's super simple, just wrap the <img>tag with an anchor tag <a>:

这非常简单,只需<img>用锚标签包裹标签<a>

<a href="myUrl.html">
    <img id="logo" src="nsc/logo2.png" alt="logo" />
</a>

This will turn the entire image into a link for you :)

这会将整个图像变成一个链接给你:)

回答by Brandito

If you can't wrap the image in a link, an image map should work just as well without requiring JavaScript & without needing to wrap the element.

如果您无法将图像包装在链接中,则图像映射应该可以在不需要 JavaScript 且不需要包装元素的情况下也能正常工作。

回答by dhellryder

I add id in img tag like this:

我在 img 标签中添加 id 像这样:

  <img id={logo.id} src={logo.url} onError={this.handleImageError} />

Since I am working on React that's why I used JSX in the id and src attribute. onError attribute is used if the image is broken. It gives me an event, So this is how I access the id attribute:

由于我正在研究 React,这就是我在 id 和 src 属性中使用 JSX 的原因。如果图像损坏,则使用 onError 属性。它给了我一个事件,所以这就是我访问 id 属性的方式:

handleImageError = (e) => { console.log(e.target.id) }

回答by SaurabhLP

Here is the solution for you,

这是给你的解决方案,

<img id="logo" src="nsc/logo2.png" alt="logo" onClick="window.open('http://yahoo.com');" />