Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何删除子元素单击事件?_Javascript_Events - Fatal编程技术网

Javascript 如何删除子元素单击事件?

Javascript 如何删除子元素单击事件?,javascript,events,Javascript,Events,这件事需要你的帮助。在导航栏中,我有一个名为author的项目,它以下拉形式显示单击时有关author的信息。问题是下拉列表是可点击的,这使得它消失了。我只想在导航栏中显示/关闭它 <div id = 'Author' class = 'navbarItem'>Author <div id = 'authorInfo'> // info about author </div> 已尝试将authorInfo的onclick eve

这件事需要你的帮助。在导航栏中,我有一个名为author的项目,它以下拉形式显示单击时有关author的信息。问题是下拉列表是可点击的,这使得它消失了。我只想在导航栏中显示/关闭它

<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';     
}