How to prevent .detach from trying to .detach elements that don't exist in the DOM

*Context* – jQuery widget factory, rendering elements and storing them in private variables. _renderInputHolder: function () { var self = this; this._inputHolder = $(document.createElement('div')) .on('focusout', function (e) { self._hideInputHolder.apply(self, [e]); }); }, _renderTextInput: function () { var self = this; this._textInput = $(document.createElement('input')) .keypress(function (e) { if (e.which === 13) { self._hideInputHolder(); } }); }, _hideInputHolder: function () { this._inputHolder = this._inputHolder.detach(); }, *Problem* – two separate elements have independent events that try to detach the container element. When the enter keypress occurs on the text input, it detaches the inputContainer but this also causes a 'focusout' event to be triggered on the inputContainer, resulting in a Uncaught Error: NotFoundError: DOM Exception 8 as it tries to detach it again. What's the best way to ensure the inputContainer's removal without error OR check that .detach() can be called?
you can hold a state variable using data() to see whether the element is detached

以上就是How to prevent .detach from trying to .detach elements that don't exist in the DOM的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » jQuery 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏