代码之家  ›  专栏  ›  技术社区  ›  Graham Conzett

jquery ui可拖动的“stop”事件调用过多次?

  •  3
  • Graham Conzett  · 技术社区  · 14 年前

    我有一种感觉,我要么误解了“停止”事件,要么做得不对,但当元素被拖动时,它似乎被多次调用。

    makeAllDragable = function () {
        $(".test-table").draggable({
            start: function (event, ui) { $(this).click(); },
            stop: function (event, ui) { foo() }
        }).click(function () {
            selectTable($(this));
        });
    }
    
    foo = function () {
        alert("test");
    }
    

    在这个例子中,foo被调用了大约30次,难道不应该只是在我释放draggable的时候?但jquery文档实际上并没有说出其中一个位置或另一个位置。

    1 回复  |  直到 14 年前
        1
  •  2
  •   Graham Conzett    14 年前

    事实证明,我在原始问题中编写的上述代码并不是我正在使用的代码,我实际上使用的foo()是这样调用的:

    makeAllDragable = function () {
        $(".test-table").draggable({
            start: function (event, ui) { $(this).click(); },
            stop: function (event, ui) { foo() }
        }).click(function () {
            foo(); /*difference here*/
        });
    }
    

    无论出于什么原因,当函数也出现在stop()事件中时,拖动它都会重复地引发click()函数。至少表面上是这样的。