dsbd 2017-07-14 13:46 采纳率: 0%
浏览 1880

如何解决JQuery中mouseenter()事件与mouseleave事件冲突

 <!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <div id="id1">
    <p>id</p>
  </div>
<script src="https://code.jquery.com/jquery-1.7.2.js"></script>
<script>$('#id1').mouseenter(function(){
  $('p').text('change-id');
  $('#id1').attr('id','id2');
  //alert($('#dsbdo').attr('id'));
});
$('#id2').mouseleave(function(){
  $('p').text('cmoeback');
  $('#id2').attr('id','id1');
  alert($('#id').attr('id'));
})</script>
</body>
</html>

当鼠标移动进div区域时mouseenter事件能够正常触发,但是当鼠标移出div时,为什么mouseleave就不能正常触发呢?求教各位大佬,感激不尽

  • 写回答

3条回答 默认 最新

  • CN-cheng 2017-07-15 00:40
    关注

    因为加载时没有#id2,所以在你把#id1变为#id2时并没有绑定mouseleave,你把mouseenter里加入$('#id2').mouseleave就可以了

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog