JS의 이벤트는 기본적으로 버블 링되며 레이어별로 전파 된 층입니다. stoppropagation () 함수를 통해 DOM 레벨에서 이벤트 전파를 중지 할 수 있습니다. 다음 예에서와 같이 :
HTML 코드 :
<! docType html> <html> <head> <meta charset = "utf-8"/> <title> stopPropagation ()-QIONGTAI Blog </title> </head> <body> <button> 버튼 </button> </body> </html> [/code] no stoppropagation () document.getEmentsByTagName ( 'button') [0]; button.onclick = function (event) {alert ( 'button click');}; document.body.onclick = function (event) {alert ( 'body click');}DOM은 레이어별로 층을 전파하므로 버튼을 클릭하는 것도 바디 층으로 퍼지므로 바디 층의 클릭도 응답합니다. 결과적으로 두 개의 경고 상자, 즉 버튼과 몸매가 나타납니다.
추가 된 stoppropagation ()
var button = document.getElementsByTagName ( 'button') [0]; button.onclick = function (event) {alert ( 'button click'); // DOM 이벤트 계층 전파 이벤트 중지 .StOpPropAgation ();}; document.body.onclick = function (event) {alert ( 'body click');}STOPPROPAGATION () 함수는 버튼의 클릭 이벤트 처리 기능에서 이벤트 전파 기능을 중지하기 위해 사용되므로 버튼 클릭 이벤트의 경고 상자가 팝업되면 본문을 전파 할 수 없으며 신체의 경고 상자가 다시 나타나지 않습니다. 결과적으로 경고 상자는 한 번만 논의됩니다.
JS를 작성할 때 많은 어린이 신발은 종종 층에 의해 층이 퍼지는 DOM 이벤트의 특성을 무시하여 프로그램의 이상을 초래합니다. 더 심층적 인 지식을 알아야한다면 JS 이벤트의 버블 링에 대한 정보를 찾을 수 있습니다.