[英]How to execute functions properly in jquery
我難以獲得一個jQuery函數來表現我想要的。 我相信懸停處理程序已被多次注冊,導致其反復觸發,但是我不確定如何正確執行。 任何幫助將不勝感激。
function hoverOptions(target)
{
var option = target.children('.option');
var tooltip = option.children('.tooltip');
option
.fadeToggle(150, function() { console.log('option'); })
.hover(function() { console.log('tooltip'); });
};
$(document).on('hover', '#myElement', function()
{
$(hoverOptions($(this)));
});
我認為您正在兩次注冊此活動。
function hoverOptions(target)
{
var option = target.children('.option');
var tooltip = option.children('.tooltip');
option
.fadeToggle(150, function() {console.log('option');)}
.hover(function(){console.log('tooltip')});
};
$(document).on('hover', '#myElement', function()
{
hoverOptions($(this));
};
如果要在jQuery名稱空間中聲明方法:
$.hoverOptions = function(target){ //Your code }
當您似乎要調用懸停事件時,您正在注冊它們。
另外,您僅將事件委派給一個元素。 在事件中涉及許多元素的情況下,事件委托才是真正的好主意。
尚不清楚您想要什么,但是我對此有所猜測(請注意簡化的代碼)。
//on hover to #myElement, fade toggle the .option child
$('#myElement').on('hover', function() {;
$(this).children('.option').fadeToggle(150, function() {
console.log('option');
});
});
//on hover to .option elements, toggle the .tooltip child
$('.option').hover(function() { $(this).children('.tooltip').toggle(); });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.