鉴于下面显示的以下 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/