Jquery onclick on div

前端 未结 6 1498
被撕碎了的回忆
被撕碎了的回忆 2021-01-01 09:04

I have a simple question. The following code works for all the tags:

$(\'*\').click(function(e) {  
    alert(1);
});

But, when I try this

6条回答
  •  我在风中等你
    2021-01-01 09:12

    Make sure it's within a document ready tagAlternatively, try using .live

    $(document).ready(function(){
    
        $('#content').live('click', function(e) {  
            alert(1);
        });
    });
    

    Example:

    $(document).ready(function() {
        $('#content').click(function(e) {  
          alert(1);
        });
    });
    #content {
        padding: 20px;
        background: blue;
    }
    
    
    Hello world

    As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers.

    $('#content').on( "click", function() {
        alert(1);
    });
    

提交回复
热议问题