javascript - TypeError : $(. ..).gridstack 不是函数 #709

标签 javascript jquery html gridstack

这是我的脚本序列:

src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.1/underscore-min.js"
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"
src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.5.0/lodash.min.js"
src="../dist/gridstack.js"
src="gridstack.js"
src="../dist/gridstack.min.js"
href="../dist/gridstack.css"
href="../dist/gridstack.min.css"

这是我的 JavaScript 代码,我在其中使用了 gridstack 函数,但它显示错误。请检查脚本和 JavaScript 文件的顺序。

    $(function () {
 alert("1");
    $('.grid-stack').gridstack({
        animate: true,
        auto: true,
        width: 12,
        float: true,
        vertical_margin: 0,
        always_show_resize_handle: /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent),
        resizable: {
            handles: 'e, se, s, sw, w'
        }   
    });

    $('.grid-stack-placeholder').remove();

    this.grid = $('.grid-stack').data('gridstack');

    this.add_widget = function(){
     alert("OK");

        var id = getRandomInt(1111,9999);

        var el = $.parseHTML("<div><div class=\"grid-stack-item-content\" data-id=\""+id+"\"/><div/>");
        this.grid.add_widget(el, 0, 0, 6, 5, true);

        $('.grid-stack-item-content[data-id="'+id+'"]').append('<span class="fa fa-times remove-widget"> </span><span class="fa fa-pencil select-use"></span>  </span><span class="fa fa-plus add-nested-widget-box"></span> ');

    }.bind(this);

    this.clear_grid = function () {
        this.grid.remove_all();
    }.bind(this);

    $('.clear-customization, .customize-pages-list li').click(this.clear_grid); 
    $('.add-widget-box').click(this.add_widget);

});

$('body').on('click', '.remove-widget', function(){

    var grid = $('.grid-stack').data('gridstack');

    grid.remove_widget($(this).parents().eq(1));

});


function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

最佳答案

将css放在上面而不是js文件

 href="../dist/gridstack.css"

最后

src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" 
src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"
src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.1/underscore-
src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.5.0/lodash.min.js"
src="../dist/gridstack.js"

从你的js中删除那些gridstack.min.js,因为你使用的是它的开发版本

即使这不起作用,也试试这个

<script>
$.noConflict();


  $('.grid-stack').gridstack({
        animate: true,
        auto: true,
        width: 12,
        float: true,
        vertical_margin: 0,
        always_show_resize_handle: /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent),
        resizable: {
            handles: 'e, se, s, sw, w'
        }   
    });

关于javascript - TypeError : $(. ..).gridstack 不是函数 #709,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45006135/

相关文章:

jquery - 我无法启用和禁用表单输入字段

javascript - 动态添加的表行不删除

javascript - Mongoose - 循环嵌入文档数组以将新值全部推送到字段?

javascript - 当 ASP.NET MVC 5 应用程序中的表单有效时触发 jQuery 函数

Javascript 使用 For 循环创建动态表,其中包含要在其中显示的数组

echo 中的php if语句

javascript - 使用 angular-ui-router 获取服务中的路由参数

javascript - readFragment 导致 IntrospectionFragmentMatcher 错误,尽管没有使用接口(interface)或联合

javascript - 通过 AJAX 调用将返回的 JSON 分配给本地存储

jQuery .offset 视口(viewport)顶部问题