Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用.live()和item.length绑定事件失败_Javascript_Jquery_Live - Fatal编程技术网

Javascript 使用.live()和item.length绑定事件失败

Javascript 使用.live()和item.length绑定事件失败,javascript,jquery,live,Javascript,Jquery,Live,我正在为我的网站制作一个小的工具提示功能 我的工作非常完美:(在项目上悬停检查气泡是否存在并显示它。如果不存在,发出请求,附加气泡,然后显示它) $('.profileIcon')。悬停(函数(){ var u=$(本); var url=$(this.find('a').attr('href')+'#intro_usuario'; if($(this).find('.nube').length>0){ $(this.find('.nube').show(); }否则{ //$('').load

我正在为我的网站制作一个小的工具提示功能

我的工作非常完美:(在项目上悬停检查气泡是否存在并显示它。如果不存在,发出请求,附加气泡,然后显示它)

$('.profileIcon')。悬停(函数(){
var u=$(本);
var url=$(this.find('a').attr('href')+'#intro_usuario';
if($(this).find('.nube').length>0){
$(this.find('.nube').show();
}否则{
//$('').load(url).addClass('nube').css({'left':$(this).offset().left+$(this).outerWidth(true)+15,'top':$(this.offset().top}).appendTo(this.show();
$(“”).addClass('nube').addClass('nubeU').load(url,function(){
$(this.css({'left':-20,'top':-16}).appendTo(u.show();
});
}
});
它只会在用户第一次悬停时发出请求并附加toolip(.nube)div,下次只会显示它(无请求)

但是在使用ajax加载更多元素时,我不得不回忆起它,所以我考虑使用live

$('.profileIcon').live('hover',function(){
    var u = $(this);
    var url = $(this).find('a').attr('href')+' #intro_usuario';

    if($(this).find('.nube').length>0){
        $(this).find('.nube').show();
    } else {
       //$('<div>').load(url).addClass('nube').css({'left':$(this).offset().left+$(this).outerWidth(true)+15,'top':$(this).offset().top}).appendTo(this).show();
       $('<div>').addClass('nube').addClass('nubeU').appendTo(u).html($('#load').html()).load(url,function(){
           $(this) .css({'left':-20,'top':-16}).show();
       });
    }
});
$('.profileIcon').live('hover',function()){
var u=$(本);
var url=$(this.find('a').attr('href')+'#intro_usuario';
if($(this).find('.nube').length>0){
$(this.find('.nube').show();
}否则{
//$('').load(url).addClass('nube').css({'left':$(this).offset().left+$(this).outerWidth(true)+15,'top':$(this.offset().top}).appendTo(this.show();
$('').addClass('nube').addClass('nubeU').appendTo(u).html($('.#load').html()).load(url,function()){
$(this.css({'left':-20,'top':-16}).show();
});
}
});
每次都会执行请求,每次都会附加一个额外的气泡

问题是:


为什么
if($(this).find('.nube').length>0
停止使用live工作?

我不想把时间浪费在
.live()
上,继续使用新的替代品

.on()
的工作原理类似于直接绑定到事件,但也类似于live。有关如何直接绑定和“气泡绑定”的详细信息,请阅读文档

尝试将
.live()
替换为
.on()
,它们应该可以正常工作

对于直接绑定(将处理程序附加到元素):

对于“气泡绑定”(请注意,
元素
现在是第二个参数,父元素被赋予处理程序。子元素将被触发,事件将向上气泡到父元素以处理事件)


此外,您不必担心使用
时该
是谁。on()
,它将始终是您触发的元素,无论是直接触发的还是气泡触发的。

以前不知道
.on()
方法。很好的提示!+1这是最近的(1.7),我上个月才知道。我不清楚您为什么要使用
。hover()
,而不是
.mouseenter()
,因为当您只将一个函数传递给
.hover()
时,当鼠标进入和离开元素时都会调用它。
$('.profileIcon').live('hover',function(){
    var u = $(this);
    var url = $(this).find('a').attr('href')+' #intro_usuario';

    if($(this).find('.nube').length>0){
        $(this).find('.nube').show();
    } else {
       //$('<div>').load(url).addClass('nube').css({'left':$(this).offset().left+$(this).outerWidth(true)+15,'top':$(this).offset().top}).appendTo(this).show();
       $('<div>').addClass('nube').addClass('nubeU').appendTo(u).html($('#load').html()).load(url,function(){
           $(this) .css({'left':-20,'top':-16}).show();
       });
    }
});
element.on('event',function(){});
someParentElement.on('event','element',function(){});