doufu7464 2018-09-19 06:12
浏览 47
已采纳

分别更改div的内容

I'm trying to add some content inside my divs on hover, but not the CSS way.

function hover(description){
  console.log(description);
  document.getElementById('first-item').innerHTML = description;
  document.getElementById('second-item').innerHTML = description;
}
.slide {
  background: blue;
  width: 200px;
  height: 200px;
  float: left;
  margin: 10px;
}
<?php
  $content = "some text";
?>

<div>
  <div>
    <div class="slide" id="first-item" onmouseover="hover('<?php echo $content; ?>')" onmouseout="hover('')"></div>
    <div class="slide" id="second-item" onmouseover="hover('<?php echo $content; ?>')" onmouseout="hover('')"></div>
    <div class="slide" id="third-item"></div>
  </div>
</div>

I want only the content of the .first-item to change when I hover it and only the content of the .second-item to change when I hover it. But as you can see when I hover either one, the content of both changes.

What did I do wrong?

</div>
  • 写回答

4条回答 默认 最新

  • doushang2023 2018-09-19 06:21
    关注

    You can pass this as a element reference in hover function for both mouse in and mouse out so that you can then use this reference in JavaScript to set innerHTML of that particular element:

    function hover(elem, description) {
      elem.innerHTML = description;
    }
    .slide {
      background: blue;
      width: 200px;
      height: 200px;
      float: left;
      margin: 10px;
    }
    <div>
        <div>
          <div class="slide" id="first-item" onmouseover="hover(this, 'sampleText')" onmouseout="hover(this, '')"></div>
          <div class="slide" id="second-item" onmouseover="hover(this, 'sampleText')" onmouseout="hover(this, '')"></div>
          <div class="slide" id="third-item" onmouseover="hover(this, 'sampleText')" onmouseout="hover(this, '')"></div>
        </div>
      </div>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路