简体   繁体   English

鼠标悬停父项时如何获取子项的ID

[英]How to get the ID of a child when mouseover parent

I guess that my question is quite simple but I'm a real beginner in javascript and I can't find what I am looking for : 我想我的问题很简单,但我是javascript的真正初学者,我找不到我要找的东西:

I am trying to get the ID of a li when mouse is over the nav or ul... My HTML structure would be : 当鼠标在nav或ul上时,我试图获取li的ID ...我的HTML结构将是:

<nav><ul id="menu">
<li id="FirstLink">Link1</li>
<li id="SecondLink">Link2</li>
<li id="ThirdLink">Link3</li>
</ul></nav>

So my goal is to listen for a mouseover (and mouseout) event on each li, but a script with 10 listener (for 5 li) is too much dirty... 所以我的目标是在每个li上监听mouseover(和mouseout)事件,但是一个带有10个监听器(5 li)的脚本太脏了......

That's why I thought of a script like : 这就是为什么我想到一个像这样的脚本:

var menu = document.getElementById("menu");
menu.addEventListener('mouseover', myFunction, false);

function myFunction () {
//something that get the ID of the <li> that is currently under the mouse and can put it inside a variable as "Link1"
}

But if there is a better solution, I will be happy to know it ! 但如果有更好的解决方案,我会很高兴知道它! (I would like to stay in pure js) (我想留在纯粹的js)

var menu = document.getElementById("menu");
menu.addEventListener('mouseover', myFunction, false);

function myFunction (event) {
    var li = event.target;
    if( li.nodeName !== 'li' ) return;

    // do your stuff with li
}

To get the ID of the hovered element you need to use event.target . 要获取悬停元素的ID,您需要使用event.target
For that you need to pass event as a parameter in your function. 为此,您需要将event作为函数中的参数传递。
Then you can get the .id attribute of that element. 然后,您可以获取该元素的.id属性。

function myFunction(event) {
    show_result.innerHTML = event.target.id;
}

Demo here 在这里演示

put the event on child elements only 仅将事件放在子元素上

var menu = document.getElementById("menu");
for( var counter = 0; counter< menu.childNodes.length; counter++)
{
    menu.childNodes[ counter ].addEventListener('mouseover', function(){
      alert( "id is  " + this.id  ); 
    }, false);
}

http://jsfiddle.net/neuroflux/ssSnN/ http://jsfiddle.net/neuroflux/ssSnN/

and

$('#menu').children("li").on('mouseover', function() {
    var childID = $(this).attr("id");
    //YOUR ID WILL BE childID!
});

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

相关问题 如何在子进程排序时获取父ID - How to get the parent id when the child is sort mouseover parent = find()子ID名称 - mouseover parent = find() child id name 单击父级时获取子元素的id - get id of child element when parent is clicked 如果父级有边框,当鼠标退出父级和子级时,如何避免来自父级元素的mouseover事件 - How to avoid the mouseover event from the parent element when the mouse is exiting both parent and child if the parent has a border 如何通过单击jQuery中的孩子来获取父母的ID - how to get the id of parent by clicking child in jquery 当 cursor 将子元素留给 hover 父元素时,如何防止父元素鼠标悬停? - How to prevent the mouseover of parent element when the cursor leaves child element to hover parent element? 当我们单击子复选框时,如何获取父母的父母ID - How to get the parent id of the parent when we click on the child check box 鼠标悬停在子元素上,而不是在父元素上 - Mouseover on child element, not on parent 父 div 事件处理程序订阅该类时如何获取子元素的 id - How to get id of child element when parent div event handler subscribed to the class 当任何孩子点击新创建的元素时,如何获取父元素的ID - How to get the ID of a parent element when any child clicked on newly created elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM