简体   繁体   English

在纯Javascript中找到event.target ID

[英]find event.target ID in pure Javascript

this is my menu HTML: 这是我的菜单HTML:

<header style="padding:0;height:2em;margin-bottom:0.5em;">
 <div id="menuDiv" class="dropdown" style="float:left;padding-top:0.4em;" >
    <svg id="menuIcon" style="width:2em;height:1.4em;">
        <line x1="0" y1="0.1em" x2="2em" y2="0.1em" class="menuSVG" />
        <line x1="0" y1="0.6em" x2="2em" y2="0.6em" class="menuSVG" />
        <line x1="0" y1="1.1em" x2="2em" y2="1.1em" class="menuSVG" />
    </svg>
    <div id="menuContent">
        <button class="buttonList">blah</button>
        <button class="buttonList">bla blah</button>
        <button class="buttonList">bla</button>
        <button class="buttonList">blah</button>
    </div>
 </div>
....

</header>

while CSS :hover property and Javascript mouseover and mouseenter events work very differently on touch devices with chrome, IE or Safari, I decided to make my menu dropdown like this: To show dropdown there is function: 尽管CSS:hover属性以及Javascript mouseover和mouseenter事件在带有chrome,IE或Safari的触摸设备上的工作方式大不相同,但我决定将菜单下拉菜单设置如下:为了显示下拉菜单,有以下功能:

document.getElementById("menuDiv").addEventListener("mouseover",showMenu);

function showMenu() {
  document.getElementById("menuContent").style.display="block";
  document.getElementById("menuIcon").style.display="none";
}

To hide dropdown I want use mouseover event working on whole window except <div id="menuDiv"> . 为了隐藏下拉菜单,我想在整个窗口上使用mouseover事件,除了<div id="menuDiv"> Code is: 代码是:

window.addEventListener("mouseover", hideMenu);
function hideMenu(event) {
    var x = document.getElementById("menuContent");
    var y = document.getElementById("menuIcon");
    let e = event || window.event;
    let target = e.srcElement.id;//HERE IS PROBLEM
    if (target !=="menuDiv") {
      //if (x.style.display==="block") {
        x.style.display="none";
        y.style.display="block";
      //}
    }
}

I do not know if hideMenu() function is completelly wrong or I need just get somehow event ID (ID of element, where mouse is pointed). 我不知道hideMenu()函数是否完全错误,或者我只需要获取某种事件ID(元素ID,指向鼠标的位置)。 There are some solutions, but only in jQuery. 有一些解决方案,但仅在jQuery中。

NOTE: this approach is justified in this case, it's nasty and not good for all problems. 注意:这种方法在这种情况下是合理的,这很讨厌并且不能解决所有问题。 use mouseleave and mouseenter in other cases. 在其他情况下,请使用mouseleave和mouseenter。

As you told me in comments you want a function that fires when mouseover anywhere except your div. 正如您在评论中告诉我的那样,您需要一个将鼠标悬停在除div以外的任何位置时触发的函数。

Let's see, when the mouse enters the div, it triggers window mouseover event and div mouseover event and it's avoidable. 让我们看看,当鼠标进入div时,它会触发窗口mouseover事件和div mouseover事件,这是可以避免的。 But you can create a way that checks if just one of them is triggered. 但是您可以创建一种方法来检查是否仅触发了其中之一。

First, we need to set a variable with 0 value, then when div mouseover triggers we set this to 1: 首先,我们需要设置一个值为0的变量,然后当div鼠标悬停触发时,将其设置为1:

var imChecking = 0;

document.getElementById("menuDiv").addEventListener("mouseover",showMenu);

function showMenu() {
    imChecking = 1;
    document.getElementById("menuContent").style.display="block";
    document.getElementById("menuIcon").style.display="none";
}

Then we put a mouseover event on window object that increments out variable: 然后,我们在窗口对象上放置一个mouseover事件,该事件使变量递增:

window.addEventListener("mouseover",hideMenu);

function hideMenu() {
    imChecking++;
    console.log(imChecking);
    if(imChecking === 1){
       document.getElementById("menuContent").style.display="none";
       document.getElementById("menuIcon").style.display="block";
    }
   imChecking = 0;
}

We know if showMenu runs, hideMenu runs too, But if the mouse is moving outside of div element only hideMenu will run. 我们知道showMenu是否运行,hideMenu也运行,但是如果鼠标移到div元素之外,则只会运行hideMenu。

Remember: this solution code is not for production, use the approach and rewrite it. 请记住:此解决方案代码不适用于生产,请使用此方法并将其重写。

Look at this plunker 看看这个矮人

I did it as follows: 我这样做如下:

window.addEventListener("mouseover", hideMenu, false);
window.addEventListener("touchend", hideMenu, false);
function hideMenu(event) {
  var x = document.getElementById("menuContent");
  var y = document.getElementById("menuIcon");
  if (event.target.id === "menuIcon" || event.target.parentElement.id === "menuContent") {
    y.style.display="none";
    x.style.display="block";
  } else {
  x.style.display="none";
  y.style.display="block";
  }
}

I tested it on IE,Opera,Chrome,Firefox,WinMobile,iPad 我在IE,Opera,Chrome,Firefox,WinMobile,iPad上进行了测试

您可以通过以下方式获取元素的ID:

event.target.id

Use modern event like mouseenter en mouseleave here. 在此处使用mouseenter en mouseleave等现代事件。

 document.getElementById("menuDiv").addEventListener("mouseenter",showMenu); function showMenu() { document.getElementById("menuContent").style.display="block"; document.getElementById("menuIcon").style.display="none"; } document.getElementById("menuDiv").addEventListener("mouseleave", hideMenu); function hideMenu(event) { var x = document.getElementById("menuContent"); var y = document.getElementById("menuIcon"); let e = event || window.event; x.style.display="none"; y.style.display="block"; } 
 <header style="padding:0;height:2em;margin-bottom:0.5em;"> <div id="menuDiv" class="dropdown" style="float:left;padding-top:0.4em;" > test <svg id="menuIcon" style="width:2em;height:1.4em;"> <line x1="0" y1="0.1em" x2="2em" y2="0.1em" class="menuSVG" /> <line x1="0" y1="0.6em" x2="2em" y2="0.6em" class="menuSVG" /> <line x1="0" y1="1.1em" x2="2em" y2="1.1em" class="menuSVG" /> </svg> <div id="menuContent"> <button class="buttonList">blah</button> <button class="buttonList">bla blah</button> <button class="buttonList">bla</button> <button class="buttonList">blah</button> </div> </div> </header> 

Another approach: 另一种方法:

 document.getElementById("menuDiv").addEventListener("mouseenter", showMenu); function showMenu() { document.getElementById("menuContent").style.display="block"; document.getElementById("menuIcon").style.display="none"; } window.addEventListener("mouseover", hideMenu); function hideMenu(event) { var x = document.getElementById("menuContent"); var y = document.getElementById("menuIcon"); let e = event || window.event; if (!document.getElementById("menuDiv").contains(e.target) ) { x.style.display="none"; y.style.display="block"; } } 
 <header style="padding:0;height:2em;margin-bottom:0.5em;"> <div id="menuDiv" class="dropdown" style="float:left;padding-top:0.4em;" > test <svg id="menuIcon" style="width:2em;height:1.4em;"> <line x1="0" y1="0.1em" x2="2em" y2="0.1em" class="menuSVG" /> <line x1="0" y1="0.6em" x2="2em" y2="0.6em" class="menuSVG" /> <line x1="0" y1="1.1em" x2="2em" y2="1.1em" class="menuSVG" /> </svg> <div id="menuContent"> <button class="buttonList">blah</button> <button class="buttonList">bla blah</button> <button class="buttonList">bla</button> <button class="buttonList">blah</button> </div> </div> </header> 

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

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