简体   繁体   English

防止在错误的元素上发生默认值,并且 mouseleave/mouseout 给出不一致的响应

[英]prevent default occurring on wrong element and mouseleave/mouseout give inconsistent response

I've tried to produce a workable snippet but this is done in wordpress and am currently unable to.我试图制作一个可行的片段,但这是在 wordpress 中完成的,目前无法。

Here's what we are trying to do.这就是我们正在尝试做的事情。 We have a menu with 5 items.我们有一个包含 5 个项目的菜单。 These are displayed in inline-block.这些显示在 inline-block 中。 On two of the five items we have a sub menu displayed in block.在五个项目中的两个上,我们有一个以块显示的子菜单。 For those two items we want to disable the click event but still have mouseover work and on mouseover display the sub menu and change the bottom margin of the navbar.对于这两个项目,我们希望禁用单击事件,但仍然有鼠标悬停工作,并且在鼠标悬停时显示子菜单并更改导航栏的底部边距。 The click event needs to work on the sub menu items.点击事件需要对子菜单项起作用。 On mouseleave we need to change the bottom margin back to 0px.在 mouseleave 上,我们需要将下边距改回 0px。

The structure of the menu is as follows:菜单结构如下:

<div class="navbar">
   <ul class="main-menu">
      <li><a href="https:\\google.com">Home</a></li>
      <li class="aboutUs">About Us
         <ul class='sub-menu'>
            <li><a href="https:\\yahoo.com">Home</a>Our Story</a></li>
            <li><a href="https:\\yahoo.com">SWAG Leaders</a></li>
            <li><a href="https:\\yahoo.com">In The News</a></li>
          </ul>
</li>
      <li><a href="https:\\google.com">Our Work</a></li>
      <li><a href="https:\\google.com">Calendar</a></li>
      <li class="takeAction">Take Action
         <ul class='sub-menu'>
            <li><a href="https:\\yahoo.com">Home Improvement</a></li>
            <li><a href="https:\\yahoo.com">Get Involved</a></li>
            <li><a href="https:\\yahoo.com">Resources</a></li>
            <li><a href="https:\\yahoo.com">Donate</a></li>
          </ul>
</li>
</div> 

and here is the JS:这是JS:

function moveDown(event){                    
const submenus = document.getElementsByClassName('sub-menu')                      
//var vision = document.getElementsByClassName('home-vision')[0]   
var vision = document.getElementById("navbar");


var sub     

    if(event.target.innerHTML === "About Us"){
        sub=submenus[0]
    }else{
        sub=submenus[1]
    }

var rect = sub.getBoundingClientRect();         
vision.style.marginBottom = rect.height + "px"      
}


function moveUp(event){ 
//var vision = document.getElementsByClassName('home-vision')[0] 
var vision = document.getElementById("menu-1");
vision.style.marginBottom = 0   
}


(function(){


var aboutUs = document.getElementsByClassName('aboutUs')[0] 
var takeAction = document.getElementsByClassName('takeAction')[0] 

aboutUs.addEventListener("click", function(event){
event.preventDefault()
})

takeAction.addEventListener("click", function(event){
event.preventDefault()
})      

aboutUs.addEventListener('mouseover', function(event) {moveDown(event)},{passive: false})
takeAction.addEventListener('mouseover', function(event) {moveDown(event)},{passive: false})  

aboutUs.addEventListener('mouseleave', function(event) {moveUp(event)},{passive: false})
takeAction.addEventListener('mouseleave', function(event) {moveUp(event)},{passive: false})     
})()

here's a link to the site这是该网站的链接

Here are the specific issues:以下是具体问题:

1) the listeners to prevent the default action of click are on the main menu elements "about us" and "take action" but are preventing the action on the sub menu and not the main menu element 1) 阻止默认单击操作的侦听器位于主菜单元素“关于我们”和“采取行动”上,但阻止了子菜单上的操作,而不是主菜单元素

2) mouseleave is suppose to work only when you leave the element or inner elements. 2)mouseleave 假设仅在您离开元素或内部元素时才起作用。 This seems to be working on "take action" but not on "about us" where it looks like it's working like a mouseout event.这似乎适用于“采取行动”,而不是“关于我们”,它看起来就像一个鼠标悬停事件。

I am trying to provide a working example.我正在尝试提供一个工作示例。 It's for a really good cause and your help is greatly appreciated.这是一个非常好的事业,非常感谢您的帮助。

UPDATE:更新:

looking at this piece of code:看这段代码:

<li class="aboutUs">About Us
         <ul class='sub-menu'>
            <li><a href="https:\\yahoo.com">Home</a>Our Story</a></li>
            <li><a href="https:\\yahoo.com">SWAG Leaders</a></li>
            <li><a href="https:\\yahoo.com">In The News</a></li>
          </ul>
</li>

We want to disable the click on the words "About Us" (which is not a link) but still have the click enabled on the ul sub menu我们想要禁用点击“关于我们”(这不是一个链接),但仍然在 ul 子菜单上启用点击

In this part of your code在这部分代码中

aboutUs.addEventListener("click", function(event){
    event.preventDefault()
})

You could check event.target and verify it's the correct click to prevent like您可以检查 event.target 并验证它是正确的点击以防止类似

aboutUs.addEventListener("click", function(event){
    if(event.target.innerText === "About Us") {
         event.preventDefault()
    }
})

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

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