如何检查鼠标是否以纯javascript输入iframe

How to check if mouseenter an iframe in pure javascript?

本文关键字:javascript 输入 iframe 是否 鼠标 何检查 检查      更新时间:2023-09-26

我确信这是可能的,因为我能够使用jQuery来完成。

$('iframe').mouseenter(function(){
    alert('jquery-work')
});
document.getElementById('iframe').addEventListener('mouseenter',function(){
    alert('vanilla-js not prompt')
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<iframe id=iframe src=http://www.bing.com></iframe>
Try to enter and leave the ifame<br>
I want the vanilla js to execute, but only js function execute

这就是您想要的吗?http://jsfiddle.net/0uz19cmu/3/

document.getElementsByTagName('iframe')[0].onmouseenter = function() {
    alert('vanilla-js not prompt');
};

这将只获得文档中的第一个iframe。如果你想得到一个特定的,那么我建议你使用id

只看上面的代码,jQuery调用不是在寻找ID为iframe的元素,而是在寻找任何iframe元素。如果您将id="iframe"添加到您的iframe,那么您的vaniall代码应该可以工作。

jQuery中的底层代码实际上是

$('#iframe') //element with the id of iframe

$('iframe') //Any iFrame elements

$('.iframe') //element with the class of iframe

答案是:

document.getElementById('iframe').addEventListener('mouseenter',function(){
alert('vanilla-js not prompt')
})

http://jsfiddle.net/0uz19cmu/4/