[英]Simulating/Call a .mouseover event with JavaScript
我想在鼠标光标位于不同元素上时在 css 选择器上发生鼠标悬停事件。 我需要通过运行 js 的 Google Tag Manager 中的自定义标签来设置它。 我的想法是:
(function() {
document.getElementById("atcclick").mouseover();
})();
当我使用.click
时,可以识别 #atcclick 上的点击,效果非常好! 但是使用.mouseover
不会识别鼠标悬停事件。 知道我做错了什么吗?
好的,感谢 epascarello 我明白了,这是有效的:
(function() {
document.getElementById('atcclick').dispatchEvent(new MouseEvent('mouseover', { 'view': window, 'bubbles': true, 'cancelable': true }));
})();
You should use.onmouseover() method instead of.mouseover()
。
做类似的事情 -
(function() { document.getElementById("atcclick").onmouseover = function(){ console.log('on mouseover event')}; })();
<p id="atcclick">This is demo text</p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.