Javascript 如何删除子元素单击事件?
这件事需要你的帮助。在导航栏中,我有一个名为author的项目,它以下拉形式显示单击时有关author的信息。问题是下拉列表是可点击的,这使得它消失了。我只想在导航栏中显示/关闭它Javascript 如何删除子元素单击事件?,javascript,events,Javascript,Events,这件事需要你的帮助。在导航栏中,我有一个名为author的项目,它以下拉形式显示单击时有关author的信息。问题是下拉列表是可点击的,这使得它消失了。我只想在导航栏中显示/关闭它 <div id = 'Author' class = 'navbarItem'>Author <div id = 'authorInfo'> // info about author </div> 已尝试将authorInfo的onclick eve
<div id = 'Author' class = 'navbarItem'>Author
<div id = 'authorInfo'>
// info about author
</div>
已尝试将authorInfo的onclick event设置为null,但无效。在侦听器上添加事件参数,以测试是否在Container而不是其子对象上单击<代码>事件。目标保存单击的元素
const author=document.getElementById('author');
const authorInfo=document.getElementById('authorInfo');
//您需要函数上的事件参数
author.onclick=函数(事件){
//仅当单击的元素是author div而不是其子元素时才执行此操作
if(event.target==作者){
如果(authorInfo.style.display='none'| | authorInfo.style.display=='')
authorInfo.style.display='block'
其他的
authorInfo.style.display='none';
}
}
作者
//关于作者的信息
您可以在此场景中使用event.stopPropagation()技术。您也尝试过,但没有预期的结果。明白了,感谢您的帮助。
const author = document.getElementById('Author');
const authorInfo = document.getElementById('authorInfo');
author.onclick = () => {
if(authorInfo.style.display == 'none' || authorInfo.style.display == '')
authorInfo.style.display = 'block'
else
authorInfo.style.display = 'none';
}