我有以下mouseover
功能:
$('.msg_id').live("mouseover", function() {
$(this).css('cursor', 'pointer');
tid = $(this).attr('id');
idx = $(this).attr('name');
resp="";
$.ajax({
async: false,
url: "log_msg.asp",
data: $("#msgForm").serialize() + "&aktion=popup&msg_id="+tid+"&msg_id"+idx,
success: function(data){
$("#"+tid).html(data);
}
});
//$.post("log_msg.asp", $("#msgForm").serialize() + "&aktion=popup&msg_id="+tid+"&msg_id"+idx,
//function(data) {
//}).success(function(){
//$("#"+tid).html(data);
//resp=data;
//$('#bub'+tid).css('display', 'block');
//popd.css('display', 'block');
//});
});
它将一些 html 代码放入 .msg_id
中( $("#"+tid).html(data);
)。
函数mouseover
在循环中被调用。当鼠标悬停在其上时,ajax 请求会一直发送,而不仅仅是一次。
我该如何修复它?
我也尝试过 mouseenter
,但它也会循环触发。
最佳答案
您可能想使用mouseenter()相反,鼠标悬停将在元素内的每次移动时触发。
$('.msg_id').live("mouseenter", function() {
//Do work here
});
或者如果不需要现场直播,只需:
$('.msg_id').mouseenter(function() {
//Do work here
});
<强> MouseOver() :
- 进入元素时触发,可以在任何子元素内部触发。
<强> MouseEnter():
- 将在进入某个元素时触发,并且仅在该元素内触发。
关于javascript - jQuery 中的鼠标悬停事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7285902/