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調試,專注於設置斷點,使用控制台和分析性能。

本文探討了Java收藏框架的有效使用。 它強調根據數據結構,性能需求和線程安全選擇適當的收集(列表,設置,地圖,隊列)。 通過高效優化收集用法

本文說明瞭如何使用源地圖通過將其映射回原始代碼來調試JAVASCRIPT。它討論了啟用源地圖,設置斷點以及使用Chrome DevTools和WebPack之類的工具。

本教程將介紹如何使用 Chart.js 創建餅圖、環形圖和氣泡圖。此前,我們已學習了 Chart.js 的四種圖表類型:折線圖和條形圖(教程二),以及雷達圖和極地區域圖(教程三)。 創建餅圖和環形圖 餅圖和環形圖非常適合展示某個整體被劃分為不同部分的比例。例如,可以使用餅圖展示野生動物園中雄獅、雌獅和幼獅的百分比,或不同候選人在選舉中獲得的投票百分比。 餅圖僅適用於比較單個參數或數據集。需要注意的是,餅圖無法繪製值為零的實體,因為餅圖中扇形的角度取決於數據點的數值大小。這意味著任何占比為零的實體

Python和JavaScript開發者的薪資沒有絕對的高低,具體取決於技能和行業需求。 1.Python在數據科學和機器學習領域可能薪資更高。 2.JavaScript在前端和全棧開發中需求大,薪資也可觀。 3.影響因素包括經驗、地理位置、公司規模和特定技能。
