jquery:mouseleave 事件似乎在不应该触发的时候触发

标签 jquery mouseleave mouseenter

鉴于下面显示的以下 html 表和脚本,我遇到了一个问题,即鼠标离开事件似乎在鼠标进入后立即触发,即使我没有将鼠标移出该行。

<script type="text/javascript" language="javascript">
    function highlightRows(iMainID) 
    {
        $('tr[mainid=' + iMainID+ ']').each(function() {
            if ($(this).attr('old') == undefined) {
                $(this).attr('old', $(this).css('backgroundColor'));
            }
            $(this).animate({ backgroundColor: "#FFFFCC" }, 500);
            $(this).mouseout(function() {
                if ($(this).attr('old') != undefined) {
                    $(this).animate({ backgroundColor: $(this).attr('old') }, 500);
                }
            });
        });        
    }
</script>
<table>
    <tr> 
      <td mainid="1" onmouseover='highlightRows(1)'><div>text</div></td>
      <td mainid="1" onmouseover='highlightRows(1)'><div>text</div></td>      
      <td mainid="2" onmouseover='highlightRows(2)'><div>text</div></td>
    </tr>
<table>

最佳答案

正如 Pointy 和 Jer 所指出的,您应该选择一种模型 (JQuery) 或另一种模型(HTML 中的 onWhatever),不要混合使用它们。

您的重复输入很可能与您多次订阅同一事件有关。 (如果您将鼠标悬停两次,您将获得两个将被调用的鼠标移出事件处理程序。)

此外,请注意重复的“mainid”值。这看起来像是一个问题,并且可能是导致您出现问题的原因。

关于jquery:mouseleave 事件似乎在不应该触发的时候触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2403876/

相关文章:

javascript - Jquery 未加载 ASP.NET

javascript - 是否有更标准的方法来处理 "this"而不使 "that"引用 "this"

javascript - 验证 10 位手机号码和焦点输入字段无效

javascript - 无法识别 reactJS 框架 mouseOver 和 mouseEnter,但 onClick 工作正常

php - 创建自动登录用户的第二个登录页面

jquery - 快速的鼠标进入/鼠标离开弄乱了动画

javascript - JQuery mouseenter() 和 mouseleave()

javascript - Firefox 中的 mouseenter、mouseleave

javascript - 悬停时简单幻灯片放映,鼠标离开时幻灯片放映停止

jquery - Angular mouseenter 在 Chrome 中不起作用