Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何让我的函数在单击时执行_Javascript_Jquery - Fatal编程技术网

Javascript 如何让我的函数在单击时执行

Javascript 如何让我的函数在单击时执行,javascript,jquery,Javascript,Jquery,我有一个简单的函数来循环一堆div,使它们淡入淡出。它工作得很好,但我试图使函数在单击DIV时也能执行 我想如果我做类似的事情 $('.content').click(InfiniteRotator()); 或 但是没有运气和建议是最感激的 $(window).load(function() { function InfiniteRotator() { //initial fade-in time (in milliseconds) var init

我有一个简单的函数来循环一堆div,使它们淡入淡出。它工作得很好,但我试图使函数在单击DIV时也能执行

我想如果我做类似的事情

$('.content').click(InfiniteRotator());

但是没有运气和建议是最感激的

$(window).load(function() {

    function InfiniteRotator() {

        //initial fade-in time (in milliseconds)
        var initialFadeIn = 1000;

        //interval between items (in milliseconds)
        var itemInterval = 5000;

        //cross-fade time (in milliseconds)
        var fadeTime = 1000;

        //count number of items
        var numberOfItems = $('.quote').length;

        //set current item
        var currentItem = 0;

        //show first item
        $('.quote').eq(currentItem).fadeIn(initialFadeIn);

        //loop through the items
        var infiniteLoop = setInterval(function(){
         $('.quote').eq(currentItem).fadeOut(fadeTime);

            if(currentItem == numberOfItems -1){
                 currentItem = 0;
            } else{
                 currentItem++;
            }

            $('.quote').eq(currentItem).fadeIn(fadeTime);

        }, itemInterval);
    }

    InfiniteRotator();


    $('.content').click(InfiniteRotator());


});

只需传递该函数的
引用
,如下所示

$('.content').click(InfiniteRotator);
顺便说一下,正如其他人提到的,您的以下代码没有问题

$('.content').click(function(){
   InfiniteRotator();
});

只需传递该函数的
引用
,如下所示

$('.content').click(InfiniteRotator);
顺便说一下,正如其他人提到的,您的以下代码没有问题

$('.content').click(function(){
   InfiniteRotator();
});
现在这两种方法都很好,但您必须在doc ready wrapper中调用它,并且您可以将全局范围内的函数移动到doc ready之外:

function InfiniteRotator() {
   // your function
}

$(function(){
   $('.content').click(InfiniteRotator); //<---here you can call it
});
function InfiniteRotator(){
//你的职能
}
$(函数(){
$('.content')。单击(InfiniteRotator)//
现在这两种方法都很好,但您必须在doc ready wrapper中调用它,并且您可以将全局范围内的函数移动到doc ready之外:

function InfiniteRotator() {
   // your function
}

$(function(){
   $('.content').click(InfiniteRotator); //<---here you can call it
});
function InfiniteRotator(){
//你的职能
}
$(函数(){

$('.content')。单击(InfiniteRotator);/
$('.content')。单击(InfiniteRotator);
将引用的函数用作处理程序,而不是此处未定义的返回值。但您的第二次尝试必须使用匿名函数。请注意,您的函数在全局范围内不可用。
$('.content')。单击(InfiniteRotator);
将引用函数用作处理程序,而不是此处未定义的返回值。但您的第二次尝试必须使用匿名函数。请注意,您的函数在全局范围内不可用。Op已使用window onload处理程序。也就是说,使用document ready处理程序更为合理,因为您建议itOP已使用window onload处理程序。也就是说,按照您的建议使用文档就绪处理程序更为合理