jQuery函数内调用用户定义的函数

时间:2023-01-06 19:14:22

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)上执行,它与函数相冲突,因此它的行为是出乎意料的,而且似乎没有执行。

#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)上执行,它与函数相冲突,因此它的行为是出乎意料的,而且似乎没有执行。