gpt4 book ai didi

javascript - 浏览器事件 : mouseover doesn't happen when you make element visible (and mouse is on it)

转载 作者:行者123 更新时间:2023-11-29 20:27:14 26 4
gpt4 key购买 nike

因此,当您使元素可见时,Mouseover 不会发生,问题是如何“触发它”?

但是!这取决于浏览器以及我无法捕捉到的其他细节:

例如检查这个 SO 片段或 Codepen https://codepen.io/rpokrovskij/pen/GRRamJe .您将需要移动鼠标来触发“鼠标悬停”。

并在那里检查相同的内容:https://dashboardcode.github.io/BsMultiSelect/snippetMouseover这里 mouseover 将在没有 mousemove 的情况下被触发。但在 IE11 或 Edge 中检查它:mouseover 将不会被触发,直到您重新输入该元素..

所以我们观察了当元素变得可见时发生的 3 种变体:

  1. 发生鼠标悬停(孤立的 github 测试示例中的 Chrome)
  2. mouseover 发生在元素内部的鼠标移动上(Chrome inside SO snippet, codepen)
  3. 鼠标悬停根本不会发生(鼠标应该离开并返回元素 - 只有这样 - IE11,github 文件示例中的 Edge)

所以更广泛的问题是“如何标准化依赖于元素的 mouseover 的代码变得可见”?

附言那里的标准行为是什么? SO 和 codepen 对与 github“隔离”文件片段有区别的事件流做了什么?

var typeMeInput = document.getElementById("typeMe");
var showMeDiv = document.getElementById("showMe");

typeMeInput.addEventListener('input',
function(){
showMeDiv.style.display="block";
}
)

for(var i =1; i<=5; i++ )
{
var itemX= document.getElementById("item"+i);
(function(itemX,i){
itemX.addEventListener('mouseover',
function(){
itemX.style.backgroundColor='yellow';
console.log("mouseover happen "+i);
}
)})(itemX, i)
}
1 put focus<br/>
2 move mouse pointer to yellow area, <br/>
3 and type any char <br/>

<input id="typeMe" style="width:100px;" />

<div style="background-color: yellow; width:100px; height:100px;">
<div id="showMe"
style="position: absolute; z-index: 1; display:none;">
<div id="item1" style="background-color: red;"><div style="width:100px;">item 1</div></div>
<div id="item2" style="background-color: red;"><div style="width:100px;">item 2</div></div>
<div id="item3" style="background-color: red;"><div style="width:100px;">item 3</div></div>
<div id="item4" style="background-color: red;"><div style="width:100px;">item 4</div></div>
<div id="item5" style="background-color: red;"><div style="width:100px;">item 5</div></div>
</div>

</div>

最佳答案

IE 在 onmouseover 方面似乎有很多问题。

使用 onmouseenter 应该使 IE 像行为 2 一样运行。

行为 1 似乎不正确,因为 onmouseover 应该只触发 "when the user moves the mouse over a particular element" .

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>var typeMeInput = document.getElementById("typeMe");
var showMeDiv = document.getElementById("showMe");

typeMeInput.addEventListener('input',
function(){
showMeDiv.style.display="block";
}
)

for(var i =1; i<=5; i++ )
{
var itemX= document.getElementById("item"+i);
(function(itemX,i){
itemX.addEventListener('mouseenter',
function(){
console.log("mouseenter happen "+i);
}
)})(itemX, i)
}</code></pre>
<pre class="snippet-code-html lang-html prettyprint-override"><code><html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=9;IE=10;IE=Edge,chrome=1"/>
</head>
<body>
1 put focus<br/>
2 move mouse pointer to yellow area, <br/>
3 and type any char <br/>

<input id="typeMe" style="width:100px;" />

<div style="background-color: yellow; width:100px; height:100px;">
<div id="showMe"
style="background-color: red; position: absolute; z-index: 1; display:none;">
<div id="item1"><div style="width:100px;">item 1</div></div>
<div id="item2"><div style="width:100px;">item 2</div></div>
<div id="item3"><div style="width:100px;">item 3</div></div>
<div id="item4"><div style="width:100px;">item 4</div></div>
<div id="item5"><div style="width:100px;">item 5</div></div>
</div>

</div>
</body>
</html></code></pre>
</div>
</div>

关于javascript - 浏览器事件 : mouseover doesn't happen when you make element visible (and mouse is on it),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59022563/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com