javascript - 如何将 "inject"变量转入匿名函数

标签 javascript jquery jquery-ui closures anonymous-function

我有以下 jQuery (ui) 代码:

    function initSortable() {
    var placeholderClass = "ui-state-highlight";
    var movement; /* = {'id': 0, 'from': 0, 'to': 0};*/
    setPlaceholderClassHeight(placeholderClass);
    $(".listTable td").each(function () {
        $(this).css("width", $(this).width());
    });
    $('.listTable tbody').sortable({
        placeholder: placeholderClass,
        forcePlaceholderSize: true,
        axis: "y",
        cursor: "grab",
        opacity: 0.8,
        start: function (event, ui) {
            ui.item.toggleClass("highlight");
            movement.id = ui.item.data('id');
            movement.from = ui.item.data('id');
            console.log(ui.item.index());
            console.log(ui.item.data('id'));
        },
        stop: function (event, ui) {
            ui.item.toggleClass("highlight");
        },
        update: function (event, ui) {
            console.log(ui.item.index());
            movement.to = ui.item.index();
            console.log(movement);
        }
    });
}

它不起作用,因为分配给启动/停止/更新的函数中的运动是未知的。 如何使用和更改运动变量?

最佳答案

movement 在正确的位置声明并且应该更新。它没有更新,因为它是未定义。声明 var moving = {} 并检查

关于javascript - 如何将 "inject"变量转入匿名函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29696893/

相关文章:

jquery - 使用 FitText 调整 FontAwesome 图标的大小

javascript - 在迭代中设置对象

javascript - 我可以显示:none part of a jQuery UI draggable and still keep the cursor in place somehow?吗

php - 如何在 PHP 中通过 AJAX 更新两个 MySQL 表?

css - .ui-state-hover a 中的 ovrreride anchor 样式来自 jqueryui css

javascript - jQuery - 焦点在 iframe 内?

javascript - Node.js:如何在不执行模块的情况下加载模块?

javascript - 使用 expo : ‘react-navigation does not exist in the haste module map’ react native

javascript - jQuery 的 ReplaceWith 的原型(prototype)替代方案

javascript - 对同一页面上的多个 html 表单重用 JavaScript 脚本