如何禁用子元素触发的mouseout事件?


在本文中,我们将学习如何使用“mouseleave”事件或原生HTML事件的stopPropagation方法来禁用子元素触发的mouseout事件。

当在子元素上触发“mouseout”事件时,它会在其DOM层次结构中冒泡,因此它也会在其所有父元素上触发相同的事件。当我们只想监听父元素本身的“mouseout”事件,而不是其子元素冒泡的事件时,这可能是多余的。

让我们了解如何通过两种不同的方法来避免上述问题

方法一

为了防止上述不需要的行为,我们可以在父元素上监听“mouseleave”事件,因为与“mouseout”事件不同,“mouseleave”事件不会冒泡,因此它可以用于分别捕获父元素和子元素实际触发的正确事件。

示例

让我们通过一个例子来了解上述方法。

文件名:index.html

<html lang="en">
<head>
   <title>How to disable mouseout events triggered by child elements?</title>
</head>
<body>
  <h3>How to disable mouseout events triggered by child elements?</h3>
  <div id="parent" style="width: 300px; height: 300px; background-color: #ccc;">
      <div id="child" style="width: 100px; height: 100px; background-color: #999;"></div>
   </div>
  
   <script>  
      const parentElement = document.getElementById('parent');
      const childElement = document.getElementById('child');

      childElement.addEventListener('mouseout', () => {
         console.log('mouseout on child element');
      });

      parentElement.addEventListener('mouseleave', () => {
         console.log('mouseleave on parent element');
      });

   </script>  
</body>
</html>

方法二

在这种方法中,我们将使用mouseout事件的stopPropagation方法来阻止其默认的冒泡行为,从而阻止子元素触发的mouseout事件出现在父元素上。

示例1

让我们通过一个例子来了解上述方法:

文件名:index.html

<html lang="en">
<head>
   <title>How to disable mouseout events triggered by child elements?</title>
</head>
<body>
   <h3>How to disable mouseout events triggered by child elements?</h3>
   <div id="parent" style="width: 300px; height: 300px; background-color: #ccc;">
      <div id="child" style="width: 100px; height: 100px; background-color: #999;"></div>
   </div>
  
   <script>  
      const parentElement = document.getElementById('parent');
      const childElement = document.getElementById('child');

      childElement.addEventListener('mouseout', (event) => {
         event.stopPropagation();
         console.log('mouseout on child element');
      });

      parentElement.addEventListener('mouseout', () => {
         console.log('mouseout on parent element');
      });
   </script>  
</body>
</html>

示例2

在这个例子中,我们将遵循上述代码模式,并使用CSS pointer-events属性禁用mouseout事件

文件名:index.html

<html lang="en">
<head>
   <title>How to disable mouseout events triggered by child elements?</title>
   <style>
      .disable-mouseout {
         pointer-events: none;
      }
   </style>
</head>
<body>
   <h3>How to disable mouseout events triggered by child elements?</h3>
   <div id="parent" style="width: 300px; height: 300px; background-color: #ccc;">
      <div id="child" style="width: 100px; height: 100px; background-color: #999;"></div>
   </div>
  
   <script>
      const parentElement = document.getElementById('parent');
      const childElement = document.getElementById('child');

      childElement.addEventListener('mouseover', () => {
         parentElement.classList.add('disable-mouseout');
      });

      childElement.addEventListener('mouseout', () => {
         parentElement.classList.remove('disable-mouseout');
      });

      parentElement.addEventListener('mouseout', () => {
         console.log('mouseout on parent element');
      });
   </script>
</body>
</html>

示例3

在这个例子中,我们将遵循上述代码模式,并使用JavaScript event.preventDefault()禁用mouseout事件:

文件名:index.html

<html lang="en">
<head>
   <title>How to disable mouseout events triggered by child elements?</title>
</head>
<body>
   <h3>How to disable mouseout events triggered by child elements?</h3>
   <div id="parent" style="width: 300px; height: 300px; background-color: #ccc;">
      <div id="child" style="width: 100px; height: 100px; background-color: #999;"></div>
   </div>
  
   <script>
      const parentElement = document.getElementById('parent');
      const childElement = document.getElementById('child');

      childElement.addEventListener('mouseout', (event) => {
         event.preventDefault();
      console.log('mouseout on child element');
      });

      parentElement.addEventListener('mouseout', () => {
         console.log('mouseout on parent element');
      });
   </script>
</body>
</html>

结论

在本文中,我们学习了如何使用两种方法来禁用子元素触发的mouseout事件。在第一种方法中,我们使用了mouseleave事件,因为它不会像mouseout事件那样冒泡。在第二种方法中,我们使用了mouseout事件的stopPropragation方法来阻止mouseout事件的冒泡。

更新于:2023年8月2日

839 次浏览

启动您的职业生涯

通过完成课程获得认证

开始学习
广告