簡體   English   中英

從回調函數-JQuery獲取返回值

[英]Getting returned value from a callback function -JQuery

我是jQuery的新手,我知道這可能超級簡單。 我想從事件中的回調函數獲取返回的值。 代碼在這里:

$('.hintItem').on('mouseenter', function(e){
     changeItemStyle(e);
     var hintItemIndex = $(this).index();
     return hintItemIndex;
});

我想獲取hintItemIndex的值並將其存儲到新變量中。 有人能幫我嗎?

嘗試這個:

var hintItemIndex;
$('.hintItem').on('mouseenter', function(e){
     changeItemStyle(e);
     hintItemIndex = $(this).index();
});

基本上,您可以在函數之外定義一個變量,然后通過函數為它分配一個值。

嘗試以下功能。

function getHintItemIndex() {
    var retVal;
    $('.hintItem').on('mouseenter', function(e){
     changeItemStyle(e);
     var hintItemIndex = $(this).index();
     return hintItemIndex;
     });
    return retval;
}

var retVal = getHintItemIndex();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM