我想自动阅读一些html元素。但是它们仅在父div被人类悬停时显示。
我已经尝试在这些div上触发鼠标事件,但是它不起作用。
function triggerMouseEvent (node, eventType) {
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent (eventType, true, true);
node.dispatchEvent (clickEvent);
}
答案 0 :(得分:0)
这是简单的代码:
function myFunction(event) {
var x = document.createEvent("MouseEvent");
x.initMouseEvent("mouseover", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
document.getElementById("myDiv").dispatchEvent(x);
}
<!DOCTYPE html>
<html>
<body>
<style>
div {
padding:50px;
background-color: Tomato;
color: white;
}
</style>
<h1>The createEvent() Method</h1>
<p>The createEvent() method allows you to simulate any event.</p>
<p>In this example, the red div will get a new star every time you mouse over it:</p>
<div onmouseover="this.innerHTML += '*';" id="myDiv">*</div>
<br>
<button onclick="myFunction(event)">Simulate Mouse Over</button>
</body>
</html>