【问题标题】:Focus/blur event not getting triggered焦点/模糊事件未触发
【发布时间】:2020-06-09 11:13:48
【问题描述】:

我正在尝试将“焦点”事件绑定到文本框,这样每次焦点到达它时,它都应该调用一个函数。但是,它不会触发焦点事件。当我将光标移入文本框时,什么也没有发生。每当单击按钮时,都会触发与“单击”事件绑定的其他事件。

下面是代码sn-p:

P.when('A',  'ready').register('dom-updater', function(A) {
var $ = A.$;
var render = function () {
    if (state.widgetType === "maker") {
        $('#saveData').bind('click', function(event) {
            saveInfoData();
        });
        $('#verifyBtn').bind('click', function(event) {
            validateData();
        });

   //This line does not work
    $( "#textBoxId" ).focus(function() {
        console.log( "Handler for .focus() called." );
    });
} else {
   //Do something else
}
};

A.on.ready(function() {
    render();
});
}

我尝试过使用“模糊”和“焦点”,但它不起作用。

$('#textBoxId').bind('blur', function(event) {
      console.log("IN blur call");
 });

为什么文本框没有附加到焦点事件?

【问题讨论】:

  • textBoxId 何时添加到 DOM?

标签: javascript jquery jquery-ui frontend focus


【解决方案1】:
$('body').on('focus', '#textBoxId', function() {
  //code
  console.log("IN focus call");
});
$('body').on('blur', '#textBoxId', function() {
  //code
  console.log("IN blur call");
});

这会起作用

【讨论】:

  • 不工作。当我专注于文本框并且控制台上没有打印任何内容时,调试没有命中。
  • 从你的代码中删除这个,刷新页面,然后把这个粘贴到控制台并点击回车,检查控制台上是否打印了一些东西
猜你喜欢
  • 2013-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多