【问题标题】:Why is focusout event not triggering in jQuery with focusout method ?为什么使用 focusout 方法在 jQuery 中没有触发 focusout 事件?
【发布时间】:2013-07-13 03:45:14
【问题描述】:

我正在使用可动手操作的 jQuery 插件。当用户停止编辑单元格时,我试图拦截。当你双击一个单元格时,一个带有handsontableInput 类的文本区域会出现在该单元格的位置上。

使用 jQuery,我尝试在用户单击其他地方时获取回调,从而失去了文本区域的焦点。

这里是简单的聚焦代码:

$(".handsontableInput").focusout(function () {
    alert("LLL");
});

这是我的fiddle

另外,如果带有handsontable 的页面位于框架中,并且我在框架外单击,这会起作用吗?

谢谢

【问题讨论】:

    标签: jquery events handsontable


    【解决方案1】:

    您应该使用.on 方法:

    $("#exampleGrid").on('focusout','.handsontableInput',function () {
        alert("LLL");
    });
    

    JSFIDDLE:http://jsfiddle.net/edi9999/HEH5C/1/

    您的代码的问题在于,在执行 $(".handsontableInput") 时,它们不是此类的元素,因此事件未附加到任何元素。

    【讨论】:

    • 感谢您的解决方案和解释。我不明白为什么它不起作用。现在说得通了。谢谢。
    【解决方案2】:

    这样的委托活动:

    $("#exampleGrid").on('focusout',".handsontableInput",function () {
        console.log("LLL");
    });
    

    【讨论】:

      【解决方案3】:

      您需要使用事件委托,因为.handsontableInput 是动态生成的

      $(document.body).on('focusout',".handsontableInput",function () {
          console.log("LLL");
      });
      

      演示--->http://jsfiddle.net/HEH5C/2/

      【讨论】:

        【解决方案4】:

        已经有一个可动手做的活动。 你可以使用它自己的事件:

        afterChange (changes: Array, source: String)
        

        虽然此事件的主要描述是“一个或多个单元格更改后触发回调”,但即使没有更改,它也会被调用,只是 changes 数组的 oldValuenewValue 是相同的。因此,在编辑结束并失去焦点时调用此事件,这正是您想要的。

        欲了解更多关于动手活动的详细信息,请查看here

        【讨论】:

          猜你喜欢
          • 2015-09-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-05-03
          相关资源
          最近更新 更多