Home >Web Front-end >HTML Tutorial >onmouseover onmouseout event problem_html/css_WEB-ITnose
onmouseover
Display DIV2 when the cursor is moved to DIV1, and hide when moved away. Of course, the DIV should also be displayed when the cursor is on DIV2. Supplementary DEMO code is as follows:
<html><head><title></title></head><body> <div onmouseover="ShowDiv()" onmouseout="HideDiv()"> 鼠标移到这里显示另一个DIV </div> <div id="div2" style="width:200px;height:200px;border:1px solid #f00;display:none;" onmouseover="ShowDiv()" onmouseout="HideDiv()"> <select> <option>1</option> <option>2</option> <option>3</option> </select> 随便写一些内容 <br /> 这里是需要显示的DIV </div></body><script type="text/javascript"> function ShowDiv() { document.getElementById('div2').style.display = 'block'; } function HideDiv() { document.getElementById('div2').style.display = 'none'; }</script></html>
DIV2 is outside DIV1, of course it will be hidden, you DIV2 should be overlaid on DIV1.
DIV2 is outside DIV1, so of course it will be hidden. You should cover DIV2 on top of DIV1.
I think this has nothing to do with DIV1. DIV1 is just to trigger the display of DIV2
The key is that after clicking the drop-down box in DIV2, it seems that the cursor has moved out of DIV2, so Onmouseout in DIV2 is triggered, so it is hidden.
Did you find that according to your div layout, your second div is displayed below the first div.
Did you find that according to your div layout, your second div is displayed below the first div.
Yes, it doesn’t matter where it is displayed. This is just a DEMO to illustrate the problem. If you move the cursor to DIV2, DIV2 will still be displayed, but when you click on the drop-down box inside DIV2, it will be hidden.
<div onmouseover="ShowDiv()" onmouseout="HideDiv(event)"> 鼠标移到这里显示另一个DIV </div> <div id="div2" style="width:200px;height:200px;border:1px solid #f00;display:none;" onmouseover="ShowDiv()" onmouseout="HideDiv(event)"> <select> <option>1</option> <option>2</option> <option>3</option> </select> 随便写一些内容 <br /> 这里是需要显示的DIV </div></body><script type="text/javascript"> function ShowDiv() { document.getElementById('div2').style.display = 'block'; } function HideDiv(e) { var e = e || window.event; var tg = e.target || e.srcElement; if(tg.tagName == "DIV"){ document.getElementById('div2').style.display = 'none'; } } </script>
<div onmouseover="ShowDiv()" onmouseout="HideDiv(event)"> 鼠标移到这里显示另一个DIV </div> <div id="div2" style="width:200px;height:200px;border:1px solid #f00;display:none;" onmouseover="ShowDiv()" onmouseout="HideDiv(event)"> <select> <option>1</option> <option>2</option> <option>3</option> </select> 随便写一些内容 <br /> 这里是需要显示的DIV </div></body><script type="text/javascript"> function ShowDiv() { document.getElementById('div2').style.display = 'block'; } function HideDiv(e) { var e = e || window.event; var tg = e.target || e.srcElement; if(tg.tagName == "DIV"){ document.getElementById('div2').style.display = 'none'; } } </script>
Um, I don’t understand what you mean. I tried it. Clicking on the drop-down box DIV2 did not disappear. I don’t know how you did it. I did it according to your div layout. My mouse was in It can't be used in div2, because div2 is below div1. If div1 appears, won't div2 be hidden? How can I click on the drop-down box?
Um, I don't understand what you mean. I tried it and clicked on the drop-down box. The box DIV2 has not disappeared. I don’t know how you did it. I did it according to your div layout. My mouse cannot enter div2 because div2 is below div1. If div1 is outside, div2 will be hidden. What else? Click on the drop-down box
无标题文档 <div onmouseover="ShowDiv()" onmouseout="HideDiv(event)"> 鼠标移到这里显示另一个DIV </div> <div id="div2" style="width:200px;height:200px;border:1px solid #f00;display:none;" onmouseover="ShowDiv()" onmouseout="HideDiv(event)"> <select> <option>1</option> <option>2</option> <option>3</option> </select> 随便写一些内容 <br /> 这里是需要显示的DIV </div></body><script type="text/javascript"> function ShowDiv() { document.getElementById('div2').style.display = 'block'; } function HideDiv(e) { var e = e || window.event; var tg = e.target || e.srcElement; if(tg.tagName == "DIV"){ document.getElementById('div2').style.display = 'none'; } } </script>
Um, I don’t understand you a bit Sorry, I tried it. Clicking the drop-down box DIV2 did not disappear. I don’t know how you did it. I did it according to your div layout. My mouse can’t enter div2 because div2 is below div1 and out of div1. , isn’t div2 hidden? How can you click on the drop-down box?
Aren’t you inconsistent? It also said that DIV2 did not disappear after clicking the drop-down box in DIV2, and that the mouse could not enter DIV2. If it could not enter, how did you click on the drop-down box?
Instead of clicking on the drop-down box, click on the drop-down box and move to another OPTION.
Aren’t you inconsistent? It also said that DIV2 did not disappear after clicking the drop-down box in DIV2, and that the mouse could not enter DIV2. If it could not enter, how did you click on the drop-down box?
Instead of clicking on the drop-down box, click on the drop-down box and move to another OPTION.
I just put div2 inside div1
Aren’t you inconsistent? It also said that DIV2 did not disappear after clicking the drop-down box in DIV2, and that the mouse could not enter DIV2. If it could not enter, how did you click on the drop-down box?
Instead of clicking on the drop-down box, click on the drop-down box and move to another OPTION.
I just put div2 inside div1
It may be a browser compatibility issue. I am using IE8 and there is no problem when I put DIV2 underneath
The problem is solved, as xiaofanku said solution, thank you