简体   繁体   English

为什么我的将项目追加到砌体中的代码给我“未定义的不是函数”?

[英]Why does my code for appending items to masonry give me 'undefined is not a function'?

I'm working on a website that uses masonry to laid out items in a grid. 我正在一个使用砖石结构将项目布置在网格中的网站上。 When the end user clicks a filter on the website, this is the function that gets called. 当最终用户单击网站上的过滤器时,就会调用此函数。 It is important that I mention right off the bat that the server side code is correct. 我必须立即提到服务器端代码正确是很重要的。 What is not correct is my jQuery code below. 不正确的是下面的我的jQuery代码。

<script>
function filter(type) {
  $.get("/storefront/?filter="+type, function(data) {
     var $data = data;
     $('.grid').children().remove();
     $('.grid').masonry().append( $data ).masonry( 'appended', $data, true ).masonry( 'layout' );
  });
}

When this function is called, the grid disappears, none of the new, filtered data appears and I receive the following error in Chrome Console for the LAST line in the function. 调用此函数后,网格消失,没有显示任何经过过滤的新数据,并且我在Chrome控制台中收到该函数中LAST行的以下错误。

uncaught typeerror: undefined is not a function

What is strange, and also proof that my server side code is correct is that if I change: 奇怪的是,如果我更改,还证明我的服务器端代码正确无误,那就是:

$('.grid').masonry().append( $data ).masonry( 'appended', $data, true ).masonry( 'layout' );

to this: 对此:

$('.grid').append( $data );

It loads in all the data and it is formatted perfectly other than the entire grid being moved up 100px. 它加载了所有数据,并且格式化了完美的格式,而不是将整个网格向上移动了100px。

The line of code causing the error is an exact replica of MANY projects that use masonry and have it working, so I don't understand why mine is not, I don't know if it is a simple syntax error or something deeper than that. 导致该错误的代码行是使用砖石结构并且可以正常工作的许多项目的精确副本,所以我不明白为什么我的不是,我不知道这是一个简单的语法错误还是更深层的错误。

As a workaround I tried to just destroy the masonry lay out and initialize a new one but I'm not even sure how to do that because the code to initalize masonry in the first place is actually inside my Animonscroll.js library. 作为一种解决方法,我试图破坏砌体布局并初始化一个新的砌体,但是我什至不知道该怎么做,因为首先将砌体初始化的代码实际上在我的Animonscroll.js库中。 Here is that part of the code (which I'm not able to follow). 这是代码的一部分(我无法理解)。

animonscroll.js

    imagesLoaded( this.el, function() {

    // initialize masonry
    new Masonry( self.el, {
        itemSelector: 'li',

        gutter: 14,
        transitionDuration : 0
    } );

    if( Modernizr.cssanimations ) {
        // the items already shown...
        self.items.forEach( function( el, i ) {
            if( inViewport( el ) ) {
                self._checkTotalRendered();
                classie.add( el, 'shown' );
            }
        } );

The error undefined is not a function generally happens when you have an object in Javascript that is undefined , and you put parenthesis () after it, as if you're trying to invoke it like a function. 如果您在Javascript中有一个undefined对象,并且在其后加上括号() ,就好像函数试图像函数一样调用undefined ,通常undefined is not a function发生undefined is not a function错误。

For example: 例如:

var a = undefined;
a();    // undefined is not a function

Somewhere in your code (it would be much easier to see with a debugger, or if you split up your code into multiple lines), you have an object that evaluates to undefined , but you're using it as a function. 在代码中的某个位置(使用调试器查看起来会容易得多,或者将代码分成多行),您有一个对象的结果为undefined ,但是您将其用作函数。

Now, to your code - it looks like you've narrowed down the problem to this line that fails: 现在,到您的代码-看起来您已经将问题缩小到失败的这一行:

$('.grid').masonry().append( $data ).masonry( 'appended', $data, true ).masonry( 'layout' );

and this alternate line that works: 和这行的替代行:

$('.grid').append( $data );

Based on this, the only things you're evaluating as functions in the failed line is masonry and append - based on that information, one of these (or something they call internally) is undefined . 基于此,您要评估的失败行中只有功能是masonryappend -根据这些信息,其中之一(或它们在内部调用的东西)是undefined

As another debugging tip, You can also use console.log to output information, like console.log($('.grid').masonry); 作为另一个调试技巧,您还可以使用console.log输出信息,例如console.log($('.grid').masonry); , etc. If masonry is undefined , then it's likely a problem loading that script. 等等。如果undefined masonry ,则加载该脚本可能会出现问题。

Hope that helps you narrow down your problem! 希望可以帮助您缩小问题范围!

I'm not sure why you are calling masonry() before you append: 我不确定为什么要在附加之前调用masonry():

$('.grid').masonry().append( $data ).masonry( 'appended', $data, true ).masonry( 'layout' );

Try this: 尝试这个:

$('.grid').append( $data ).masonry( 'appended', $data, true ).masonry( 'layout' ); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM