2017-02-22 31 views
0

這是我的代碼不能正常工作:jQuery調用任何<a>標籤的鼠標懸停功能?

$(a).on('mouseover', function() { 
    alert('alert'); 
}); 
+0

不知道你的用例,但考慮使用「mouseenter」事件。請參閱https://api.jquery.com/mouseover/ – MicD

+0

「不起作用」是什麼意思?你會得到什麼錯誤?什麼是或不是正在發生?另外,除非'a'是一個變量,你可能的意思是$('a')' – j08691

回答

2

試試這個

$('a').on('mouseover', function() { 
    alert('alert'); 
}); 

如下包裝document.ready

$(document).ready(function() { 
    $('a').on('mouseover', function() { 
     alert('alert'); 
    }); 
}); 

它在這裏工作。 https://jsfiddle.net/3648w1gr/

+0

這仍然不起作用,我的jQuery肯定是附加的。 – user7391050

+0

如上所述嘗試在'$(document).ready'中包裝你的函數。 –

+0

看看這個。 https://jsfiddle.net/3648w1gr/ –

1

您需要更改$(a)$('a')和事件的名稱是mouseenter

$('a').on('mouseenter', function() { 
alert('alert'); 
});