javascript - 删除特定事件

标签 javascript jquery web

假设我有一个组件,inputType="text",id ="cmp";

在一个js文件中,

$("input[type='text']").bind('keypress', function(e) {
//Case 1
});

在我的jsp文件中,

$("#cmp").keypress(function() {
  //Case 2
});

现在我只需要删除一个按键事件。 是否可以删除从 js 文件注册的 id = "cmp"的按键事件。 但是我们不应该删除从 jsp 文件中注册的事件。

注意: 根据我的要求,我无法更改.js 文件。

最佳答案

您可以使用 jquery 方法的 off 来删除事件 Jquery OFf

The off() method removes event handlers that were attached with .on(). See the discussion of delegated and directly bound events on that page for more information. Specific event handlers can be removed on elements by providing combinations of event names, namespaces, selectors, or handler function names. When multiple filtering arguments are given, all of the arguments provided must match for the event handler to be removed.

$('#cmp').off('keypress');

关于javascript - 删除特定事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15331932/

相关文章:

javascript - 在事件中为链接添加样式

javascript - Stack Overflow API - 端点问题

javascript - Grails、javascript 渲染模板后不响应

c# - 使网站仅在 HTTPS 上运行

html - 尝试在工作区中绑定(bind) "/%PUBLIC_URL%/manifest.json"失败,因为在 Netlify 上运行时此 URI 格式错误

javascript - 如何取出显示在url中的div? - 添加了 JS fiddle

JavaScript 和 jQuery : iterate through an array but wait for animations to finish

javascript - 点击菜单页面跳转到顶部

jquery - 在可调整大小的父图像上定位元素

javascript - 在 JavaScript 中使用 forEach() 函数调用 $el 时出现问题