jQuery - live()

script 2010. 1. 13. 00:09
Events 함수 중에 좀 멋진 live() !
HTML상의 있지도 않은 element에 대해 이벤트를 지정할 수 있다.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            var index = 0;
            $("p.test").live("click", function() {
                alert($(this).text());
                $(this).after("<p class=\"test\">클릭" + ++index + "</p>");
            });
        });
    </script>
    <style type="text/css">
        p.test { background:#ccc; }
    </style>
</head>
<body>
    <p class="test">클릭</p>
</body>
</html>

live() 함수에 click 이벤트를 지정하여 앞으로 생길 p태그에 대해 이벤트를 지정할 수 있다.

Demo

클릭

: