我有一个工具提示,其中包含从ajax加载的数据:
$('.show_tooltip').hover(
function(){
$.post('getdata.php', function(data) {
$('#tooltip').html(data);
$('#tooltip').show()
});
},
function(){
$('#tooltip').hide();
}
);
Run Code Online (Sandbox Code Playgroud)
有用.
但问题是 - 当我将它悬停在元素上并快速移出鼠标时,所以$('#tooltip').show()没有效果,而且它hide().但是当数据加载时,它会弹出并保持显示而不会悬停.
可以做些什么呢.有没有办法完全停止发布请求?
jQuery AJAX方法返回一个jqXHR对象,该对象是本机XMLHttpRequest对象的扩展,它有一个abort停止请求的方法:
var xhr;
$('.show_tooltip').hover(
function(){
//Save reference to the jqXHR object
xhr = $.post('getdata.php', function(data) {
$('#tooltip').html(data);
$('#tooltip').show()
});
},
function(){
xhr.abort(); //Abort the request
$('#tooltip').hide();
}
);
Run Code Online (Sandbox Code Playgroud)