2

每次特定输入失去焦点时,输入字段上的 focusout 都会触发。但是,我想排除一些特定的标签来触发该 focusout 功能

例子:

<input type="text" id="name_input">
<a id="apply_name">SAVE</a>

然后是focusout函数:

$("#name_input").focusout(function(e) {
  //do something here
});

单击“#apply_name”也会触发输入的焦点输出功能。如何排除该特定元素 ID 来触发它。注意:我尝试了一些已经在 StackOverflow 上发布的技巧,但它们都没有接缝工作......

4

3 回答 3

3

另一种方法是检查您的目标 id 是什么

var evt;
document.onmousemove = function (e) {
    e = e || window.event;
    evt = e;
}
$("#name_input").focusout(function (e) {
    if (evt.target.id == "apply_name") {
        //apply_name clicked
    } else {
        //focus out and applyname not clicked
    }
});

演示

于 2014-01-13T10:51:54.887 回答
0

您可以使用“模糊” - 事件。

$("#name_input").on("blur", function(e) {
    //your code
});
于 2014-01-13T10:39:43.440 回答
0

How to exclude Id from focusout 的解决方案 添加了我还需要的 .hasClass:

$('#id').focusout (function (e) {

    if (e.relatedTarget && $(e.relatedTarget).hasClass('dontFocusOut')) {
        return;
    }
    //do your thing

});
于 2017-02-14T17:06:12.200 回答