2013-09-26 66 views
0

我試圖使用懸停與.on(),但所有代碼和答案我發現不起作用。jquery懸停與.on()

我需要使用.on,因爲它的Ajax內容。

一些香港專業教育學院的嘗試:

$(document).on('mouseenter', '[rel=popup]', function() { 
    mouseMove = true; 
    width = 415; 
    $('#tool-tip').show(); 

    var type = $(this).attr('data-type'), 
     id = $(this).attr('data-skillid'); 

    console.log("TT-open"); 
    ttAjax(type, id); 
}).on('mouseleave', '[rel=popup]', function() { 
    mouseMove = false; 
    console.log("TT-close"); 
    $('#tool-tip').hide(); 
    $('#tt-cont').html(""); 
    $('#tt-ajax').show(); 
}); 

$('[rel=popup]').on('hover',function(e) { 
    if(e.type == "mouseenter") { 
     mouseMove = true; 
     width = 415; 
     $('#tool-tip').show(); 

     var type = $(this).attr('data-type'), 
      id = $(this).attr('data-skillid'); 

     console.log("TT-open"); 
     ttAjax(type, id); 
    } 
    else if (e.type == "mouseleave") { 
     mouseMove = false; 
     console.log("TT-close"); 
     $('#tool-tip').hide(); 
     $('#tt-cont').html(""); 
     $('#tt-ajax').show(); 
    } 
}); 

$('[rel=popup]').on("hover", function(e) { 

    if (e.type === "mouseenter") { console.log("enter"); } 
    else if (e.type === "mouseleave") { console.log("leave"); } 

}); 

$(document).on({ 
    mouseenter: function() { 
     console.log("on"); 
    }, 
    mouseleave: function() { 
     console.log("off"); 
    } 
}, "[rel=popup]"); //pass the element as an argument to .on 

原非。對:

$('[rel=popup]').hover(function(){ 
    mouseMove = true; 
    width = 415; 
    $('#tool-tip').show(); 

    var type = $(this).attr('data-type'), 
     id = $(this).attr('data-skillid'); 

    console.log("TT-open"); 
    ttAjax(type, id); 

},function() { 
    mouseMove = false; 
    console.log("TT-close"); 
    $('#tool-tip').hide(); 
    $('#tt-cont').html(""); 
    $('#tt-ajax').show(); 
}) 

所有。對回報 「類型錯誤:$(...)就不是一個函數」 。我正在使用版本1.9.1。

+4

「懸停」不是一個事件。 – Pointy

+0

我也使用mouseeneter並離開,他們也不工作。 –

+2

如果你得到這個錯誤,你要麼沒有真正使用你認爲你是jQuery的版本,否則會有'$'衝突。 – Pointy

回答

2

您正在尋找的事件可能是

$("#id").mouseover(function(){}); 

$("#id").mouseout(function(){}); 
+0

「mouseenter」和「mouseleave」事件由jQuery管理,應該可以正常工作。 – Pointy

+1

儘管有上述的意見和downvote,這是不是一個壞的建議,因爲它從jQuery的1.0+工程和提問者的問題似乎是,他並沒有用他認爲的版本(或有版本之間。衝突),但我會真的建議修復版本問題並正確執行。 – Archer

0

有一個答案:

$(document).on({ 
    mouseenter: function() { 
     console.log("on"); 
    }, 
    mouseleave: function() { 
     console.log("off"); 
    } 
},"[rel=popup]"); 

這個工作,出於某種原因,我有JQ 1.4在1.9.1命名文件中導致問題。

+0

所以一個糟糕的腳本文件就是這個問題 - 與你的代碼無關。 – Archer