mouseover Bubbles, mouseenter Does Not

mouseover and mouseout bubble up from children, so moving inside a container re-fires it; mouseenter and mouseleave fire only on real boundary crossings.

JavascriptData Integrity

Code

javascriptsnippet
23 lines
// Native rule: mouseover/mouseout bubble up from children; mouseenter/mouseleave
// do not — the UA dispatches native mouseenter/mouseleave with bubbles:false.
const parent = document.createElement("div");
const child = document.createElement("span");
parent.append(child);
document.body.append(parent);

parent.addEventListener("mouseover", () => console.log("parent mouseover"));
parent.addEventListener("mouseout", () => console.log("parent mouseout"));
parent.addEventListener("mouseenter", () => console.log("parent mouseenter"));
parent.addEventListener("mouseleave", () => console.log("parent mouseleave"));

// 合成事件只考察冒泡路径:路径由构造参数 bubbles 决定,与事件类型无关。
// 对 child 派发 bubbles:true 的 mouseover → 冒泡到父级,符合原生行为
child.dispatchEvent(new MouseEvent("mouseover", { bubbles: true }));
// 对 child 派发 bubbles:true 的 mouseenter → 同样冒泡到父级:dispatchEvent
// 不套用类型原生的不冒泡语义,所以合成 mouseenter 不等价于原生 mouseenter
child.dispatchEvent(new MouseEvent("mouseenter", { bubbles: true }));
child.dispatchEvent(new MouseEvent("mouseout", { bubbles: true }));

// bubbles:false 的合成事件传播在 target 处停止,父级冒泡阶段监听器不会触发——
// 这正是原生 mouseenter/mouseleave 被 UA 以不冒泡方式派发时的路径形状
child.dispatchEvent(new MouseEvent("mouseleave", { bubbles: false }));

How it works

Demo state sits in top-level bindings: parent, child.

What to watch for

  • The whole dependency story is the language version itself.
  • A browser is the runtime here: document are not available in Node.

Putting it to use

Output lines are prefixed (parent mouseover, parent mouseout, parent mouseenter), making each step’s effect easy to spot.

Core lines

child.dispatchEvent(new MouseEvent("mouseleave", { bubbles: false }));
child.dispatchEvent(new MouseEvent("mouseenter", { bubbles: true }));
child.dispatchEvent(new MouseEvent("mouseover", { bubbles: true }));

Interactive Tools for This Topic

FAQ

Does mouseover Bubbles, mouseenter Does Not 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.