on('mouseenter')(this)。显示特定div中的文本

时间:2017-07-27 02:16:20

标签: javascript jquery html

我一直在尝试不同的方法来完成这项任务,但都没有。 基本上我一直在尝试的是当我将鼠标悬停在“$ this”(divOne,divTwo ...)div上时,文本在div“writeToMe”中弹出。已经花了几个小时,开始怀疑它是否可能,而不是进一步尝试......我以为我会这样做。

我知道下面的jquery是胡说八道,只是试图解释我想要做的事情。

<div class="writeToMe"></div>  //on hover, show text within writeToMe

    $(document).ready(function() {

$(document).on('mouseenter', "divOne", function(){
    $(writeToMe).text("Today I ate a banana")
    })
$(document).on('mouseenter', "divTwo", function(){
    $(writeToMe).text("I fell on the stairs when I woke up")
    })
$(document).on('mouseenter', "divThree", function(){
    $(writeToMe).text("I slept")
    })
$(document).on('mouseenter', "divFour", function(){
    $(writeToMe).text("Today I woke up")
    })
    });

1 个答案:

答案 0 :(得分:1)

这是你之后的事吗?

&#13;
&#13;
$(".writeToMe").mouseenter(function() {
  var txt = null;
  switch (this.id) {
    case "divOne":
      txt = "Today I ate a banana";
      break;
    case "divTwo":
      txt = "I fell on the stairs when I woke up";
      break;
    case "divThree":
      txt = "I slept";
      break;
    case "divFour":
      txt = "Today I woke up";
      break;
  }
  if (txt)
    $(this).text(txt);
});
&#13;
.writeToMe {
  border: 1px solid grey;
  min-width: 100px;
  min-height: 20px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="divOne" class="writeToMe"></div>
<div id="divTwo" class="writeToMe"></div>
<div id="divThree" class="writeToMe"></div>
<div id="divFour" class="writeToMe"></div>
&#13;
&#13;
&#13;