为什么我不能将javascript函数与jquery方法混合使用

Why I can't mix javascript functions with jquery methods?

本文关键字:方法 jquery 混合 函数 不能 javascript 为什么      更新时间:2023-09-26

我对使用jQuery很陌生(我擅长常规js),我遇到了困难,真的非常困难,试图弄清楚这一点,我已经阅读了好几天,因为我通常自己找到答案,但这已经走得太远了。

我不知道,为什么,当这样编码时:

$(document).ready(slideShow());
function slideShow() {
    alert("enters");
    $('ul.imgslider').each(function () {
        $('li', this).css("display", "block");
        $('li', this).fadeOut(1000);
    });
};

警报已执行,但 .each() 未执行。如果你像这样编码:

$(document).ready(function slideShow() {
    alert("enters");
    $('ul.imgslider').each(function () {
        $('li', this).css("display", "block");
        $('li', this).fadeOut(1000);
    });
});

一切都执行。我只是不明白,我想知道为什么会这样?更奇怪的是,在 jsfiddle 中它确实被执行了,但当我在本地运行时它不会。我正在使用jquery-2.0.3.min.js。

因为$(document).ready(slideShow())会立即调用slideShow,并将其返回值传递给ready函数。

$(slideShow);

这就是应该这样做;)