<script>
var element = document.getElementById("name");
element.addEventListener("mouseover", function () {
element.style.color = "red";
console.log("Event triggered");
});
element.addEventListener("mouseleaver", function () {
element.style.color = "black";
console.log("Event triggered");
});
var event = new MouseEvent("mouseover", {
view: window,
bubbles: true,
cancelable: true,
});
var event2 = new MouseEvent("mouseleaver", {
view: window,
bubbles: true,
cancelable: true,
});
element.dispatchEvent(event);
// 两秒后触发mouseLeaver事件
setTimeout(() => {
console.log("两秒后触发mouseLeaver事件");
element.dispatchEvent(event2);
}, 2000);
</script>