首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >显示悬停文本(反应)

显示悬停文本(反应)
EN

Stack Overflow用户
提问于 2021-12-07 10:24:31
回答 1查看 602关注 0票数 0

我有两个div,我希望它们在悬停时显示文本,但是当我悬停在其中一个上时,它们都显示文本。以下是代码:

代码语言:javascript
复制
>     const [text, setText] = useState(0);
>     <div onMouseEnter={e=> setText(1)} onMouseLeave={e=> setText(0)}>
>         <h1>hover the div to show the text</h1>
>         <p style={{opacity: `${text}`}}>some dummy text</p>
>     </div>
      <div onMouseEnter={e=> setText(1)} onMouseLeave={e=> setText(0)}>
>         <h1>hover the div to show the text</h1>
>         <p style={{opacity: `${text}`}}>some dummy text</p>
>     </div>
EN

回答 1

Stack Overflow用户

发布于 2021-12-07 11:00:23

我已经修好了,这是密码:

代码语言:javascript
复制
 const [text, setText] = useState({firstDiv:0, secondDiv: 0});
 <div onMouseEnter={e=> setText({firstDiv:1, secondDiv: 0})} onMouseLeave={e=> setText(0)}>
     <h1>hover the div to show the text</h1>
     <p style={{opacity: text.firstDiv}}>some dummy text</p>
 </div>
  <div onMouseEnter={e=> setText({firstDiv:0, secondDiv: 1})} onMouseLeave={e=> setText(0)}>
     <h1>hover the div to show the text</h1>
     <p style={{opacity: text.secondDiv}}>some dummy text</p>
 </div>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70258452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档