简体   繁体   English

onClick 未在子 div 上调用

[英]onClick not called on child div

I have an 'outermost' or 'parent' div that has two child divs:我有一个“最外层”或“父”div,它有两个子 div:

1) the 1st child div is visible on page load, inside the parent div (I put borders around these divs to keep visual track of them), and this 1st child div simply contains a text string. 1) 第一个子 div 在页面加载时可见,位于父 div 内(我在这些 div 周围放置边框以保持对它们的视觉跟踪),并且第一个子 div 仅包含一个文本字符串。

2) the other div is "display:none" at page load time. 2) 另一个 div 在页面加载时为 "display:none"。 I'll call this div the "mouse hover div" below.我将把这个 div 称为下面的“鼠标悬停 div”。

3) this "mouse hover div" gets displayed on mouseover, and has an 'onclick' handler that displays some text in a div at the bottom of the page. 3) 这个“鼠标悬停 div”在鼠标悬停时显示,并且有一个“onclick”处理程序,可以在页面底部的 div 中显示一些文本。

The onmouseover event handled by the parent div hides the 1st child div (and hence its text string), then shows the 2nd, previously-hidden div by setting its "style: block".由父 div 处理的 onmouseover 事件隐藏第一个子 div(以及它的文本字符串),然后通过设置其“样式:块”显示第二个先前隐藏的 div。

As tested, when the mousemove happens over the parent div, the parent div's "onmousemove" hides the 1st div and shows the other div -- all that works fine.经测试,当 mousemove 在父 div 上发生时,父 div 的“onmousemove”隐藏第一个 div 并显示另一个 div——一切正常。

The problem is, the "mouse hover div" is never having its 'onclick' handler called when the mouse is clicked on it.问题是,当单击鼠标时,“鼠标悬停 div”永远不会调用其“onclick”处理程序。 I can tell because the 'onclick' code fails to display its text string in the div at the page's bottom.我可以说是因为“onclick”代码无法在页面底部的 div 中显示其文本字符串。

Here is the code (note: not using jquery in this project):这是代码(注意:在这个项目中不使用 jquery):

 <div id="theParentDiv" class="popupMenuFrame" style="border: 2px solid red;"
      onmouseover="displayPopup()"  onmouseout="doMOut()">

       <div id="theLocale" style="border: 2px solid green; display: inline-block">
          Austin, TX
       </div>       

       <div class="hoverDivClass" id="theHoverDiv" style="border: 2px solid purple;"
            onclick="handleMenuClick()"
            onmousedown="handleMenuClick()">
        Austin
       </div>

 </div>


    // These are for debug output at the bottom of the page
 <div id="debugOut1" style="font-weight: bold; width: 100px">debug #1</div>
 <div id="debugOut2" style="font-weight: bold; width: 100px">debug #2</div>

Here are the mouse handlers:以下是鼠标处理程序:

 function displayPopup()
 {
     var localeName = document.getElementById('theLocale');
     localeName.style.display="none";

     var thePopupMenu = document.getElementById('theHoverDiv');     
     thePopupMenu.style.display = "block";

     // this is a div at page-bottom telling me the mouse events
     var dbgport2 = document.getElementById("debugOut2");
     dbgport2.innerHTML = "showing popup....";
 }


  // THIS IS THE PROBLEM, IT'S NOT GETTING CALLED when the mouse is
  // clicked ON THE "hover" DIV
 function handleMenuClick()
 {   
     var localeName = document.getElementById('theLocale');
     localeName.style.display="block";

     var thePopupMenu = document.getElementById('theHoverDiv');     
     thePopupMenu.style.display = "none";

     var dbgport2 = document.getElementById("debugOut2");
         // THIS TEXT NEVER APPEARS.
     dbgport2.innerHTML = "got menu click!";   
 }

function doMOut()
{ 
     var dbgport1 = document.getElementById("debugOut1");
     dbgport1.innerHTML = "got mouse OUT";
     var localeName = document.getElementById('theLocale');
     localeName.style.display="block";

     var thePopupMenu = document.getElementById('theHoverDiv');     
     thePopupMenu.style.display = "none";        
 }

Here's the CSS:这是CSS:

 .popupMenuFrame
 {
     font-size: 11px;
     min-width: 28px; 
     min-height: 12px; 
 }


 .hoverDivClass
 {
     display: none;
     width: 100%;
 }

You'll notice I tried calling the mouse click handler both for onmousedown and onclick one at a time then both together in the "hover div" html code -- no change, the mouse click event handler is not being called regardless.您会注意到我尝试一次为 onmousedown 和 onclick 调用鼠标单击处理程序,然后在“悬停 div”html 代码中同时调用鼠标单击处理程序——没有变化,无论如何都不会调用鼠标单击事件处理程序。

Any ideas?有任何想法吗? I've tried many permutations of this code, it is conceptually simple, but not working, and I'm not seeing why.我已经尝试了这段代码的许多排列,它在概念上很简单,但不起作用,我不明白为什么。

BY THE WAY.顺便一提。 There is a subtle timing issue involving the showing/hiding of the "hover div" because when it appears on top of its parent div, the parent div loses focus, triggering a mouseout on the parent div, which re-hides the "hover div" immediately (see the mouseout handler on the parent div), then the child hover div re-appears when the parent div re-gets focus and the mousemove event, ad infinitum as long as the mouse is hovering above.有一个微妙的时间问题涉及“悬停 div”的显示/隐藏,因为当它出现在其父 div 之上时,父 div 失去焦点,触发父 div 上的鼠标移出,从而重新隐藏“悬停 div” " 立即(参见父 div 上的 mouseout 处理程序),然后当父 div 重新获得焦点和 mousemove 事件时,子悬停 div 会重新出现,只要鼠标悬停在上方,就无止境。

So I've already put the 'onmouseout' into the "hover div" but that didn't change the effect, so the subtlety in the code is the "hover div" is rapidly being hidden/shown as it obscures/reveals its underneath parent div due to the mouse events appearing back-and-forth on the parent div and hover div.所以我已经将“onmouseout”放入“hover div”中,但这并没有改变效果,所以代码中的微妙之处在于“hover div”正在迅速隐藏/显示,因为它隐藏/显示了它的下方由于鼠标事件在父 div 和悬停 div 上来回出现而导致父 div。

Not sure if that explains why the onclick is not happening when the mouse is clicked.不确定这是否解释了为什么单击鼠标时没有发生 onclick。

The div that handles the mousemove event and then displays the 'hover div' was interfering with the mouse handling when the 'hover div' is made invisible again. 当再次使“ hover div”不可见时,处理mousemove事件并随后显示“ hover div”的div会干扰鼠标的处理。 The mouse handling is only do-able with the 'hover div' being made visible completely not overlapping the div handling the onmousemove. 只有使“ hover div” 完全可见而不与处理onmousemove的div 重叠时 ,鼠标处理才可行。 So for now, I changed the code above so when onmousemove fires, the div responds by making the hover div visible but not overlapping the onmousemove-handling div. 所以现在,我更改了上面的代码,以便在onmousemove触发时,div通过使悬停div可见但不与onmousemove-handle div重叠来进行响应。

Just comment the below line from you function displayPopup and then click. 只需在函数displayPopup中注释以下行,然后单击即可。 You will find your mistake. 您会发现自己的错误。

dbgport2.innerHTML = "showing popup...."; //comment it.

just type event.stopPropagation();只需输入 event.stopPropagation(); inside function to work onclick on both parent and child tags内部函数在父标签和子标签上工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM