简体   繁体   English

如何在reactjs中使用document.getElementById使图像可点击

[英]How to make a image clickable using document.getElementById in reactjs

I am currently using the Twitch API, where I have created a file that renders the game cover image by searching. 我当前正在使用Twitch API,在其中创建了一个文件,该文件通过搜索来渲染游戏封面图像。 I want the user to be able to click the game image, which will redirect them to their proper Twitch Links 我希望用户能够单击游戏图像,这将使他们重定向到正确的Twitch链接

Search Response 搜索响应

搜索响应

My code for the game image rendering looks like this: 我的游戏图像渲染代码如下所示:

render() {
const { game } = this.props

return (
  <div className="GameDetails">
      <img src={this.formatImageUrl(game.box_art_url)} alt="" />
      <p>{game.name} </p>
      <p>ID: {game.id}</p>
  </div>
   )
  }
 }

export default GameImage

I tried out: 我尝试了:

render() {
  const { game } = this.props

return (
  <div className="GameDetails">
      <img src={this.formatImageUrl(game.box_art_url)} alt="" onClick${"https://www.twitch.tv/directory/game/${document.getElementById("SearchName").value}"}/>
      <p>{game.name} </p>
      <p>ID: {game.id}</p>
 </div>
   )
  }
 }

 export default GameImage

Which gives me an error. 这给了我一个错误。 The "SearchName" value is what the user types in the search bar for the game, therefore I want to send them to the respectable twitch pages when clicked. “ SearchName”值是用户在游戏的搜索栏中键入的内容,因此,我希望在单击它们时将其发送到相应的抽搐页面。

Of course you will receive an error, because firstly you've misspelled $ with = and secondly, onClick prop expects a function which will handle the action after clicking the image. 当然,您会收到一个错误,因为首先您用=拼错了$ ,其次, onClick prop需要一个函数,该函数将在单击图像后处理操作。

Suggested approach: 建议的方法:

handleClick = () => {
   // logic when user clicks on image
   // https://www.twitch.tv/directory/game/${document.getElementById("SearchName").value}
}

render() {
  const { game } = this.props

  return (
    <div className="GameDetails">
      <img src={this.formatImageUrl(game.box_art_url)} alt="" onClick={this.handleClick} />
      <p>{game.name} </p>
      <p>ID: {game.id}</p>
    </div>
  )
}

export default GameImage

Edit : It's kinda difficult to understand what you really want to achieve, however if you want that img to work as a link, you should consider using a element instead. 编辑 :这是有点难以理解你真的想达到什么样的,但是如果你要那img为纽带的工作,你应该考虑使用a元素来代替。 Just wrap your img tag as well as p into a . 只是包装你的img标签以及pa

render() {
  const { game} = this.props
  const link = `https://www.twitch.tv/directory/game/${document.getElementById("SearchName").value}`;

  return (
    <div className="GameDetails">
      <a href={link}>
        <img src={this.formatImageUrl(game.box_art_url)} alt="" onClick={this.handleClick} />
        <p>{game.name} </p>
        <p>ID: {game.id}</p>
      </a>
    </div>
  )
}

If all you want to do is go to another site by clicking on the image simply wrap it in an HTML anchor with the url as the href attribute. 如果您要做的只是通过单击图像转到另一个站点,只需将其包装在HTML锚点中,并将url作为href属性。 Hover over the images (don't click on them) to see the URL in the browser status bar. 将鼠标悬停在图像上(不要单击它们)以在浏览器状态栏中查看URL。

 function App({ data }) { return data.map(game => <Details game={game} />); } function Details({ game }) { return ( <div className="gameDetails"> <a href={game.twitch_url}> <img src={game.box_art_url} /> </a> </div> ); } const data = [ { id: 1, twitch_url: 'http://game1.com', box_art_url: 'https://dummyimage.com/100x100/000/fff' }, { id: 2, twitch_url: 'http://game2.com', box_art_url: 'https://dummyimage.com/100x100/555/fff' }, ]; ReactDOM.render( <App data={data} />, document.getElementById('container') ); 
 .gameDetails { display: inline-block; padding: 0.3em; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="container"></div> 

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

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