Skip to content

@longmo-utils/browser / off

Function: off()

ts
function off(
   element, 
   event, 
   handler, 
   useCapture): void;

事件解绑 移除元素上的事件监听器

Parameters

ParameterTypeDefault valueDescription
elementWindow | Document | HTMLElementundefined目标元素
eventstringundefined事件名称
handlerEventListenerOrEventListenerObjectundefined事件处理函数
useCapturebooleanfalse是否为捕获阶段的事件,必须与绑定时一致

Returns

void

Examples

typescript
const handler = () => console.log('Clicked');
on(button, 'click', handler);
// ... 某些条件下
off(button, 'click', handler);
typescript
// 在组件挂载时绑定
const resizeHandler = () => console.log('Window resized');
on(window, 'resize', resizeHandler);

// 在组件卸载时清理
function cleanup() {
  off(window, 'resize', resizeHandler);
}
typescript
const handlers = {
  click: null as (() => void) | null,
  scroll: null as (() => void) | null
};

// 绑定
handlers.click = () => console.log('Clicked');
handlers.scroll = () => console.log('Scrolled');
on(button, 'click', handlers.click);
on(window, 'scroll', handlers.scroll);

// 清理
if (handlers.click) off(button, 'click', handlers.click);
if (handlers.scroll) off(window, 'scroll', handlers.scroll);
typescript
const handler = () => console.log('Captured');
on(container, 'click', handler, true);
// 移除时必须也传入 true
off(container, 'click', handler, true);

Released under the MIT License.