프로그래밍/Jquery

jquery - 기본 이벤트를 제거하고 이벤트 전달 막는 법 2가지

가카리 2014. 10. 11. 16:35
반응형

기본 이벤트를 제거하고 이벤트의 전달을 막기 예제입니다.


preventDefault메소드로  기본 이벤트를 제거합니다.


stopPropagation메소드로 이벤트 전달을 제거합니다.


<!DOCTYPE html>

<html>

<head>

    <style>

        *{

        margin: 5px; padding: 5px;

        border: 3px solid black;

        }

    </style>

    <script src="http://code.jquery.com/jquery-1.10.2.js"></script>

        <script>

        $(document).ready(function (){

        //이벤트에 연결합니다.

        $('a').click(function(event){

        $(this).css('background-color', 'blue');

        event.stopPropagation();

        event.preventDefault();

        

        //아니면 다음과 같이 해도된다.

        //return false;

        });

        

        $('h1').click(function (){

        $(this).css('backgound-color', 'red');

        });

        });

        

        

    </script>

</head>

<body>

        <h1>

        <a href="http://hanb.co.kr">han media</a>

        </h1>

        

</body>

</html>



실행화면


다음과 같이 링크를 클릭해도 링크로 연결이 되지않습니다.


반응형