【问题标题】:Masonry : undefined is not a function砌体:未定义不是函数
【发布时间】:2014-07-23 13:52:18
【问题描述】:

我知道undefined is not a function 错误是什么意思,但我不明白为什么它会出现在这里。

我刚刚下载了最后一个 Masonry 版本并尝试按照文档进行操作。 (http://desandro.github.io/masonry/docs/methods.html)

布局工作正常,但似乎我根本无法使用任何masonry() 函数。在我的masonry() 调用中总是收到undefined is not a function 错误。

既然布局工作正常,我假设砌体脚本工作正常,那么为什么我不能在没有这个未定义函数错误的情况下使用masonry(...)

(我搜索了从 Masonry v2 到 Masonry v3 的变化,但没有找到与此功能相关的任何内容)

部分 HTML 代码:

<a class="navbar-brand" id ="test-masonry" href="#">...</a>

<div id="masonry-container" class="js-masonry" data-masonry-options='{ "columnWidth": 330, "itemSelector": ".item" }'>
    <div class="item">
        <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#createNewBlock">Create a new block</button> 
    </div>
</div>

<!-- Freshly downloaded from the official Masonry website -->
<script src="js/masonry.pkgd.min.js"></script> 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.min.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers/appCtrl.js"></script>

<!--  Copied from the example in the docs -->
<script>
$(function(){
    var $container = $('#masonry-container');
    $('#test-masonry').click(function(){
        var $boxes = $('<div>Test !</div>');
        $container.prepend( $boxes ).masonry( 'reload' );
    });
});
</script>

【问题讨论】:

    标签: javascript jquery masonry


    【解决方案1】:

    您在 jQuery 之前加载 Masonry,因此您得到 undefined。当包括砌体时,它会检查是否加载了 jQuery。如果是,它会将 Masonry 属性设置为 jQuery 对象:$.fn.masonry。所以当你在 jQuery 之前加载 Masonry 时,你不能使用jQObject.masonry()

    布局工作正常,因为 Masonry 不需要 jQuery 来工作

    【讨论】:

    • 谢谢,这就是问题所在。不确定我如何“加载 jQuery 两次”,但可能与 Angular 和 jQLite 有关?
    • 不知道,也许您正在使用 CMS?
    • 没有。砌体、JQuery、AngularJS 和 Bootstrap。就是这样。没关系,它现在可以工作了。
    • @ClémentMalet 很高兴知道您如何使用它来帮助其他有类似问题的人 - 我在使用 bootstrap 时遇到了同样的问题
    • @fidev 正是答案中写的内容。我在 jQuery 之前加载 Masonry,所以我将它向下移动,以便在 jQuery 之后加载它并且它可以工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    相关资源
    最近更新 更多