javascript - 如何将外部网站链接到导入的 React src 图像?

标签 javascript html reactjs

我正在使用一个使用 React 的 Gatsby 入门主题,我在代码顶部导入了一个本地镜像,并使用 img src 标记在页面的不同部分中调用它。该图像显示完美,但我希望它在我单击时转到另一个网站。

是否可以将此图片链接到外部网站?

import React from 'react'
import PropTypes from 'prop-types'

import pic03 from '../images/pic03.png'




<article id="guides" className={`${this.props.article === 'about' ? 'active' :
 ''} ${this.props.articleTimeout ? 'timeout' : ''}`} style={{display:'none'}}>

   <h2 className="major">Guides</h2>
   <span className="image main"><img src={pic03} alt="" /></span>
   <p>Lorem ipsum dolor sit amet, consectetur et adipiscing elit. Praesent eleifend dignissim arcu, at eleifend sapien imperdiet ac. </p>
   {close}
 </article>

最佳答案

在 Gatsby 中,您可以使用 Link 标签进行内部链接,但对于外部链接,您可以用 <a> 围绕图像。标记并使用 href 指向您想要的位置。像这样:<a href="http://www.google.com"><img src={pic03} alt="" /></a>

您甚至可以设置目标属性来确定是否要在新选项卡中打开,如下所示:<a href="http://www.google.com" target="_blank"><img src={pic03} alt="" /></a>

关于javascript - 如何将外部网站链接到导入的 React src 图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57873686/

相关文章:

javascript - 连续 Logo 轮播

javascript - 滚动时 jquery .animate()

html - 分配给多个类时无法使用 CSS 为 div 元素设置动画

javascript - react 触发无限请求

javascript - 如何在 TypeScript React 中向 onChange 和 onClick 函数添加类型

javascript - 如何从 javascript 中的 json 对象获取参数的名称?

javascript - 警告 : functions are not valid as a react child question. ..是因为我的容器吗?

html - 将导航栏中的内容对齐到顶部

javascript - 维基百科 3c 中的 Prims MST 没有意义

javascript - aws DynamoDB getItem InvalidParameterType 错误