【问题标题】:How to "inject" a variable into an anonymous function如何将变量“注入”到匿名函数中
【发布时间】:2015-06-24 04:02:42
【问题描述】:

我有以下 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 在分配给启动/停止/更新的函数中是未知的。 如何使用和更改运动变量?

【问题讨论】:

  • movement 在正确的位置声明,它应该更新。它没有更新,因为它是undefined。声明var movement = {} 并检查
  • 将其移至答案:)

标签: javascript jquery jquery-ui closures anonymous-function


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 2010-11-15
    相关资源
    最近更新 更多