繁体   English   中英

删除 HTML 元素上的事件

[英]Remove events on an HTML element

我有一个textarea元素。 在这堆遗留代码的某个地方,我相信注册了一个事件,阻止我在 textarea 中输入文本。 我试过下面的代码。

 $('#clinicalNotesEditable').off("keyup keydown keypress change blur"); $('#clinicalNotesEditable').on("click", function() { $(this).attr('disabled', false); $(this).attr('readonly', false); console.log('Click on textarea by ID'); // I can see the CLICK event is captured. }); $('#clinicalNotesEditable').on("keyup keydown keypress", function() { console.log('Key pressed on textarea by ID'); // I do not see this message. });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <textarea class="noteseditable" id="clinicalNotesEditable" style="width: 370px !important; vertical-align: middle; display: inline-block; -ms-overflow-y: scroll;" placeholder="click here to enter a note" rows="8"></textarea>

我唯一能想到的是我没有正确使用.off() 如何从clinicalNotesEditable元素中删除所有事件?

您可以迭代jQuery._data(<DOM element>, 'events')并删除委托和非委托事件

 $("body").on('click', '#clinicalNotesEditable', function() { console.log('delegated'); }); $("body").on('click', function() { console.log('not delegated'); }); $('#clinicalNotesEditable').on("click", function() { $(this).attr('disabled', false); $(this).attr('readonly', false); console.log('Click on textarea by ID'); }); $('#clinicalNotesEditable').on("keyup keydown keypress", function() { console.log('Key pressed on textarea by ID'); //I see this in the CONSOLE }); var selector = '#clinicalNotesEditable'; $.each([$(window), $(document), $("*")], function(key, value) { $.each(value, function(k, v) { var event = $._data(v, "events"); if (event !== undefined) { $.each(event, function(a, b) { if (b[0].selector === selector) { $(v).off(a, '**'); } else { if ('#' + v.id === selector) { $(selector).off(a); } } }); } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <textarea class="noteseditable" id="clinicalNotesEditable" style="width: 370px !important; vertical-align: middle; display: inline-block; -ms-overflow-y: scroll;" placeholder="click here to enter a note" rows="8"></textarea>

根据您在此处获得的代码顺序,如果您将代码重新排列如下:

 $('#clinicalNotesEditable').on("keyup keydown keypress", function() { console.log('Key pressed on textarea by ID'); //keyup keydown and keypress events are removed }); $('#clinicalNotesEditable').off("keyup keydown keypress change blur"); $('#clinicalNotesEditable').on("click", function() { $(this).attr('disabled', false); $(this).attr('readonly', false); console.log('Click on textarea by ID'); });

keyup keydown 按键事件被删除,您将在控制台中看到文本“Click on textarea by ID”。 我不确定这是否是您想要实现的目标。 我运行了代码,我可以在 textarea 中添加文本。

你可以 unbind() 而不是 off()

 $('#clinicalNotesEditable').off("keyup keydown keypress change blur"); $('#clinicalNotesEditable').on("click", function() { $(this).attr('disabled', false); $(this).attr('readonly', false); console.log('Click on textarea by ID'); }); $('#clinicalNotesEditable').on("keyup keydown keypress", function() { console.log('Key pressed on textarea by ID'); //I see this in the CONSOLE }); $('#clinicalNotesEditable').unbind()
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <textarea class="noteseditable" id="clinicalNotesEditable" style="width: 370px !important; vertical-align: middle; display: inline-block; -ms-overflow-y: scroll;" placeholder="click here to enter a note" rows="8"></textarea>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM