一些html元素仅在人类将鼠标悬停在其父div的鼠标上时显示

时间:2019-09-02 16:52:14

标签: javascript web-scraping

我想自动阅读一些html元素。但是它们仅在父div被人类悬停时显示。

我已经尝试在这些div上触发鼠标事件,但是它不起作用。

function triggerMouseEvent (node, eventType) {
    var clickEvent = document.createEvent ('MouseEvents');
    clickEvent.initEvent (eventType, true, true);
    node.dispatchEvent (clickEvent);
}

1 个答案:

答案 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>