首页 > 开发 > JS > 正文

focusin()绑定触发函数后,如何在focusout()销毁已绑定的内容?

2017-09-05 13:02:24  来源:网友分享

之所以用focusin(),是因为他支持父元素上检测子元素失去焦点的情况。
一个一个移除太麻烦了。

解决方案

使用 off 解绑就可以。

target.off('focusin');

示例:http://jsfiddle.net/UUPHq/

<input id="test" placeholder="点我"/> <span id="console"></span>u200bu200bu200bu200bu200bu200bu200bu200bu200bu200bu200bu200bu200bu200bu200bu200bu200bu200bu200bu200bu200bu200bu200bu200bu200bu200bu200b
var target = $('#test');  var console = $('#console');  target.focusin(function() {      console.text('focusin');  });    target.focusout(function() {      console.text('focusout');      target.off('focusin');  });u200b