stopImmediatePropagation Halts Sibling Listeners
stopPropagation still lets same-node listeners run and only blocks bubbling; stopImmediatePropagation cuts off later listeners on the same node too.
Code
// stopPropagation 只切断冒泡:同节点的兄弟监听仍会执行
const save = document.querySelector('#save');
save?.addEventListener('click', (event) => {
event.stopPropagation();
console.log('first listener ran');
});
save?.addEventListener('click', () => {
console.log('same-node listener still runs');
});
document.body.addEventListener('click', () => {
console.log('body listener blocked by stopPropagation');
});
// stopImmediatePropagation 更强:同节点的兄弟监听也一并掐断
save?.addEventListener('dblclick', (event) => {
event.stopImmediatePropagation();
console.log('dblclick first listener ran');
});
save?.addEventListener('dblclick', () => {
console.log('never runs on dblclick');
});How it works
Top-level bindings save hold the demo state — nothing sits inside a function scope.
What to watch for
- The snippet leans on
document— browser-only surfaces a bare Node process cannot provide.
Using it
The run emits 5 labeled sections — first listener ran, same-node listener still runs, body listener blocked by stopPropagation, dblclick first listener ran, never runs on dblclick — one per step.
Key statements
save?.addEventListener('dblclick', (event) => {
event.stopImmediatePropagation();
console.log('dblclick first listener ran');
});
save?.addEventListener('click', (event) => {
event.stopPropagation();
console.log('first listener ran');
});
save?.addEventListener('dblclick', () => {
console.log('never runs on dblclick');
});
Interactive Tools for This Topic
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.DOM Event Propagation VisualizerVisualize DOM event capturing, targeting, and bubbling phases in real time. Toggle stopPropagation and stopImmediatePropagation to see how events traverse the tree.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.
FAQ
- Does stopImmediatePropagation Halts Sibling Listeners run in plain Node.js?
- No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.
Report a problem with this exampleOpens a pre-filled issue on GitHub.