프로그래밍/Jquery

jquery - on메소드를 이용한 이벤트 연결

가카리 2014. 10. 11. 15:52
반응형

on 메소드를 활용한 이벤트 연결


다음예제는 h1을 클릭시 +를 추가해주는 예제입니다.


<!DOCTYPE html>

<html>

<head>

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

    <script>

        $(document).ready(function (){

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

        

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

        $(this).html(function(index, html){

        return html + '+';

        });

        });

        

        });

    </script>

</head>

<body>

        <h1>Header-0</h1>

        <h1>Header-1</h1>

        <h1>Header-2</h1>

</body>

</html>




실행화면


















on메소드를 이용한 이벤트 추가 두 번째 예제


<!DOCTYPE html>

<html>

<head>

    <style>

        .reverse {

            background: black;

            color: white;

        }

    </style>

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

        <script>

        $(document).ready(function (){

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

        

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

        $(this).html(function(index, html){

        return html + '+';

        });

        });

        

        

        $('h1').on({

        mouseenter: function() { $(this).addClass('reverse'); },

        mouseleave: function() { $(this).removeClass('reverse'); }

        

        });

        

        });

        

        

    </script>

</head>

<body>

        <h1>Header-0</h1>

        <h1>Header-1</h1>

        <h1>Header-2</h1>

</body>

</html>



실행 화면






반응형