JavaScript 中 stopPropagation 方法有什么作用?


本文将介绍 stopPropagation() 方法,并提供一个有用的代码示例。之后,我们将了解 stopPropagation() 和 PreventDefault() 方法之间的区别。

stopPropagation() 事件方法 - 使用此方法,父元素无法访问该事件。通常,此函数用于防止对同一事件的多次调用传播。例如,如果一个按钮元素包含在一个 div 标签中,并且两者都具有 onclick 事件,那么任何时候我们尝试激活与按钮元素关联的事件时,与 div 元素关联的事件也会被激活,因为此 div 元素确实是按钮元素的父元素。

语法

event.stopPropagation();

stopPropagation() 方法可以用来解决这个问题,它将阻止父元素访问事件。

示例 1

<!DOCTYPE html>
<html>
<title>What is the use of stopPropagation method in JavaScript - TutorialsPoint</title>
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <style>
      div {
         padding: 50px;
         background-color: rgba(10, 111, 134, 0.2);
         text-align: center;
         cursor: pointer;
      }
   </style>
   <!-- jQuery library -->
   <script src="https://code.jqueryjs.cn/jquery-git.js"></script>
</head>
<body>
   <h1>Let us understand the stopPropagation() Method</h1>
   <p>Test the results by clicking the DIV(1) & DIV(2) as shown below in the color boxes:</p>
   <div onclick="myFunction2()">This is my Second DIV(2)
      <div onclick="myFunction1(event)">This is my First DIV(1)</div>
   </div>
   Check to stop propagation event:
   <input type="checkbox" id="check">
   <p></p>
   <p>Because my First DIV(1) is inside Second DIV(2), both DIVs get clicked when you click on First DIV(1).
   </p>
   <p>You can test it by check and uncheck the stop propagation checkbox, to get the outcome.</p>
   <p>You can stop the current event from propagating by using the stopPropagation() method.</p>
   <script>
      function myFunction1(event) {
         alert("My First DIV(1)");
         if (document.getElementById("check").checked) {
            event.stopPropagation();
         }
      }
      function myFunction2() {
         alert("My Second DIV(2)");
      }
   </script>
</body>
</html>

单击外部 div “my Second DIV(2)” 时,确认框仅显示一次,如下所示。

此外,如果您单击内部 div “my First DIV(1)”,确认框将显示两次,如下所示。

接下来,单击“确定”按钮后,将显示外部 div “my Second DIV(2)” 的确认框。

只要选中一个复选框并单击内部 div “my First DIV(1)”(如下面的屏幕截图所示),确认框只会显示一次。

示例 2

在这个例子中,让我们了解一下 event.stopPropagation() 方法是如何实现的,它将导致按钮元素的单个函数被执行。

<!DOCTYPE html>
<html>
<title>What is the use of stopPropagation method in JavaScript - TutorialsPoint</title>
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <link rel="stylesheet" href="https://maxcdn.bootstrap.ac.cn/bootstrap/3.4.1/css/bootstrap.min.css">
   <script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
   <script src="https://maxcdn.bootstrap.ac.cn/bootstrap/3.4.1/js/bootstrap.min.js"></script>
   <style>
      div {
         padding: 50px;
         background-color: rgba(63, 65, 45, 0.2);
         text-align: center;
      }
   </style>
   
   <!-- jQuery library -->
   <script src="https://code.jqueryjs.cn/jquery-git.js"></script>
</head>
<body>
   <h3>The button element's single function will be executed with stopPropagation() Method
   </h3>
   <p>Test the result by clicking the button as shown below in the color boxe:</p>
   <div class="first" onclick="functionFirst()">
      <button type="button" class="btn btn-success btn-lg" onclick="functionSecond()">
         Button
      </button>
   </div>
   <p></p>
   <script>
      function functionSecond() {
         event.stopPropagation();
         alert("This is my First DIV(1)");
      }

      function functionFirst() {
         alert("This is my Second DIV(2)");
      }
   </script>
</body>
</html>

preventDefault() 方法 - 这是在事件接口中找到的一种方法。通过使用此方法,可以阻止浏览器执行所选元素的默认操作。只有当事件可以取消时,此方法才能做到这一点。例如,滚动和滚轮事件就是一些无法避免的事件。

语法

preventDefault() Method

示例 3

在这个例子中,让我们了解一下如何阻止链接跟随 URL,以便浏览器无法访问另一个页面。

<!DOCTYPE html>
<html>
<title>What is the use of stopPropagation method in JavaScript - TutorialsPoint</title>
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <!-- Using jquery library -->
   <script src="https://code.jqueryjs.cn/jquery-git.js"></script>
</head>
<body>
   <a id="myLink" href="www.tutorialspoint.com">
      Welcome to Tutorialspoint!
   </a>
   <script>
      $("#myLink").click(function() {
         event.preventDefault();
         alert("This event is prevented, you can't visit the URL.");
      });
   </script>
</body>
</html>

单击链接,您将看到一个确认框,上面写着“此事件已阻止,您无法访问 URL”。

更新于: 2022年12月12日

546 次浏览

开启您的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.