日期:2014-05-16  浏览次数:20450 次

jqeury 添加html 元素后 .click 失效问题
  <script>
        function xxx() {
            var abc = '<li><h3 class="geegee">3</h3></li>';
            var acc = '<li><h3 class="geegee">4</h3></li>';
            output = $(abc + acc);
            $('#category').append(output);
        }
    </script>

    <script>
        $(document).ready(function() {
            $(".geegee").click(function() {
                alert("a");
            });
        });  
    </script>
<body onload="xxx();">
        <ul id="category" class="product_category">
         <li><h3 class="geegee">1</h3></li>
         <li><h3 class="geegee">2</h3></li>
        </ul>
    </div>
</body>


原本的 1,2 有 click的功能 , onload 后添加的却没有 ,click 了没反应
这是为什么呢 ?该怎么解决 ?

------解决方案--------------------
$(".geegee").click(function() {
                alert("a");
            });

改掉
            $("#category").delegate(".geegee","click",function() {
                alert("a");
            });

能理解自己理解,不理解来问