jQuery函數內調用用戶定義的函數

[英]jQuery Calling user defined function within function


I am trying to call a user defined function within a jquery function to no avail I have tried this:

我試圖在jquery函數中調用一個用戶定義函數,但我沒有嘗試過:

function close_quote_bar() {
    alert('oh hai');
}


if($('#sliding_quote:visible')) {

    $('#sliding_quote').slideUp();
    $('#trans_div').animate({opacity: 0.0}).css('display','none').close_quote_bar;

}

and this

//Function to close quote bar
function close_quote_bar() {
    alert('oh hai');
}

$('#get_quote_bar img').click(function() {                          
    if($('#sliding_quote:visible')) {               
        $('#sliding_quote').slideUp();
        $('#trans_div').animate({opacity: 0.0}).css('display','none').close_quote_bar;

    }               
});

With not much luck! Nothing happens when I cal close_quote_bar, or I get an Object is missing method error!

沒有多少運氣!當我卡爾close_quote_bar什么也沒有發生,或者我得到一個對象缺少方法錯誤!

hope you guys can point me in the right direction I am really struggling with this one

希望你們能告訴我正確的方向,我真的很糾結這個問題。

5 个解决方案

#1


2  

I assume you want something like this:

我假設你想要這樣的東西:

$('#sliding_quote:visible').slideUp( function(){
    $('#trans_div').css('display','none');
    close_quote_bar();
} );

It uses the slide up callback to then set the object to display none and run your function. This code also does away with the if statement by instead using the selector to only affect the visible element.

它使用滑動回調來設置對象以顯示none並運行您的函數。這段代碼也不使用if語句,而是使用選擇器只影響可見元素。

#2


0  

you should try this:

你應該試試這個:

$('#trans_div').animate({opacity: 0.0}, 400, function(){
    $(this).css('display','none');
    close_quote_bar();
});

#3


0  

Building on @laurencek's answer.

基於@laurencek的回答。

$('#sliding_quote:visible').slideUp( function(){
    $('#trans_div').fadeOut( function(){
      close_quote_bar();
    });
});

this executes close_quote_bar() as a callback of fadeOut.

這個執行close_quote_bar()的回調漸隱。

#4


0  

This line of your code needs to be changed

這行代碼需要更改

$('#trans_div').animate({opacity: 0.0}).css('display','none').close_quote_bar;

You have to call it like this

你必須這樣稱呼它。

$('#trans_div').animate({opacity: 0.0}).css('display','none');
close_quote_bar();

And finally to trigger your closequotebar AFTER your animation have finished to animate, use this code, notice that a function() is in the 3rd parameter.

最后觸發你closequotebar動畫完成動畫后,使用該代碼,請注意()函數的第三個參數。

$('#trans_div').animate({opacity: 0.0}, 400, function(){
    $(this).css('display','none');
    close_quote_bar();
});

both animate and slideUp supports adding function() block as their parameters, so you could call a function after the animating has finished http://api.jquery.com/animate/ see the "complete" part

動畫和slideUp支持添加函數()塊的參數,你可以調用一個函數在動畫完成http://api.jquery.com/animate/看到“完整”的部分

#5


0  

It turns out the function was being called and it was not an issue with the scope or anything like that.

原來這個函數被調用,它不是一個問題范圍或類似的東西。

I had another function that was executing on scroll (.scroll) that was conflicting with the function hence why it was behaving unexpectedly and appeared not to be executing.

我有另一個函數在滾動(.scroll)上執行,它與函數相沖突,因此它的行為是出乎意料的,而且似乎沒有執行。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/02/06/7254643bd52a59e2f92655fb06ad2fc6.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com