2

我有一个调用 SwapOnscreenPhotos 的函数。这是该功能的来源:

function SwapOnscreenPhotos(currentPhotoSet, arrowClicked)
{
    $("#photo-list img").each(function(index) 
    { 
        $(this).attr("src", photoData[currentPhotoSet][index].url);

        if (photoData[currentPhotoSet][index].liked === true)
        {   
            $(this).addClass("liked");
        }
        else
        {
            $(this).removeClass("liked");
        }
    });

    //Animate the old photos off of the screen
    $("#match").hide("slide", { direction: "left" }, 1000);
}

我做了一些随机的事情,然后在函数结束时,我想无条件地调用 #match 上的效果。没有事件之后我要执行此效果(如果重要,事件会确定我是否首先调用 SwapOnScreenPhotos)。我应该如何执行这个效果?

编辑:

对不起,让我澄清一下自己。我的印象是每个 jQuery 表达式都遵循类似的公式。$('selector').event(function() { //function stuff});

我的代码$("#match").hide("slide", { direction: "left" }, 1000);与这个例子有很大的不同。我的代码更类似于以下模型:$('selector').function. 请注意,没有任何事件指示我的代码应该何时执行以及何时不应执行。有没有办法在没有这个事件的情况下调用 jQuery 函数?当我尝试运行上面的代码时,我得到了这个错误:

未捕获的类型错误:对象 # 的属性 '#' 不是函数 jquery.js:8780 Tween.run jquery.js:8780 tick jquery.js:8491 jQuery.fx.timer jquery.js:9032 Animation jquery.js:8555 jQuery .fn.extend.animate.doAnimation jquery.js:8871 jQuery.extend.dequeue jquery.js:1894 jQuery.fn.extend.queue jquery.js:1937 jQuery.extend.each jquery.js:611 jQuery.fn.jQuery .each jquery.js:241 jQuery.fn.extend.queue jquery.js:1930 jQuery.fn.extend.animate jquery.js:8881 jQuery.each.jQuery.fn.(匿名函数) jquery.js:8853 AnimateMatch 匹配.js:21 SwapOnscreenPhotos match.js:70 $.ajax.success match.js:167 jQuery.Callbacks.fire jquery.js:974 jQuery.Callbacks.self.fireWith jquery.js:1082 done jquery.js:7650 jQuery. ajaxTransport.send.callback

我认为此错误是由于我未能指定事件引起的。这更有意义吗?谢谢

4

2 回答 2

2

我的印象是每个 jQuery 表达式都遵循类似的公式。$('selector').event(function() { //function stuff});

不,事实并非如此。并非一切都是对事件的反应。您可以致电:

$("#match").hide("slide", { direction: "left" }, 1000);

在 DOM 准备好并且操作将在所选元素上执行之后的任何时间。

您遇到的错误可能是因为您在 DOM 准备好之前尝试使用 jQuery 元素选择。这就是为什么您通常将需要选择元素的任何内容包装在 onReady 附件中的原因。这样 js 直到 DOM 被解析并可以使用后才会执行。

$(function(){
   // this will execute as soon as the DOM is ready
   $("#match").hide("slide", { direction: "left" }, 1000);
});

onReady 与 onLoad 类似,但发生在之前,因为 onLoad 等待加载外部资产(如图像),而 onReady 在 DOM 加载后立即发生。

于 2012-09-14T03:22:52.747 回答
1

您正在尝试使用JQuery UIhide;如果尚未加载 JQuery UI 库,而您正在使用纯 JQuery ,则这是您收到的错误消息。

例如,如果我使用 Firebug 在此(堆栈溢出)页面上运行您的代码,那么我会得到相同的错误。

$("body").hide("slide", { direction: "left" });

TypeError:对象#的属性“#”不是函数


总而言之,您尝试调用该函数的方式没有任何问题。我认为问题在于您忽略了包含 JQuery UI 库,或者它没有正确加载。

于 2012-09-15T04:37:48.937 回答