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.jquery.com/jquery-git.js"></script> </head> <body> <h1 id="Let-us-understand-the-stopPropagation-Method">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“我的第二个 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.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/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.jquery.com/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.jquery.com/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。”
以上是JavaScript中的stopPropagation方法有什么用的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

本文讨论了在浏览器中优化JavaScript性能的策略,重点是减少执行时间并最大程度地减少对页面负载速度的影响。

本文讨论了使用浏览器开发人员工具的有效JavaScript调试,专注于设置断点,使用控制台和分析性能。

本文说明了如何使用源地图通过将其映射回原始代码来调试JAVASCRIPT。它讨论了启用源地图,设置断点以及使用Chrome DevTools和WebPack之类的工具。

本文探讨了Java收藏框架的有效使用。 它强调根据数据结构,性能需求和线程安全选择适当的收集(列表,设置,地图,队列)。 通过高效优化收集用法

掌握了入门级TypeScript教程后,您应该能够在支持TypeScript的IDE中编写自己的代码,并将其编译成JavaScript。本教程将深入探讨TypeScript中各种数据类型。 JavaScript拥有七种数据类型:Null、Undefined、Boolean、Number、String、Symbol(ES6引入)和Object。TypeScript在此基础上定义了更多类型,本教程将详细介绍所有这些类型。 Null数据类型 与JavaScript一样,TypeScript中的null

本教程将介绍如何使用 Chart.js 创建饼图、环形图和气泡图。此前,我们已学习了 Chart.js 的四种图表类型:折线图和条形图(教程二),以及雷达图和极地区域图(教程三)。 创建饼图和环形图 饼图和环形图非常适合展示某个整体被划分为不同部分的比例。例如,可以使用饼图展示野生动物园中雄狮、雌狮和幼狮的百分比,或不同候选人在选举中获得的投票百分比。 饼图仅适用于比较单个参数或数据集。需要注意的是,饼图无法绘制值为零的实体,因为饼图中扇形的角度取决于数据点的数值大小。这意味着任何占比为零的实体
