This question already has answers here:
How can I get the current class of a div with jQuery?
                                
                                    (11个答案)
                                
                        
                        
                            Event binding on dynamically created elements?
                                
                                    (23个答案)
                                
                        
                                3年前关闭。
            
                    
如何获得divs的类名?

<body>
    <a id="append">Add DIV</a>
    <div id="parent"></div>
    <script>
        $(document).ready(function() {
          var count = 0;
          $('#append').click(function() {
            $('#parent').append('<div
                class="article' + count + '">im div</div>');
            count++;
          });
          $('div').click(function() {
            alert(this.class);
          });
        });
    </script>
</body>


当我尝试在警报中显示div的类名称时,它显示未定义。

最佳答案

在这里使用事件委托Fiddle

$('#parent').on('click','div',function() {
className = $(this).attr('class');
    alert(className);
});

09-10 18:16