Bind multiple events to jQuery 'live' method

后端 未结 4 1997
梦谈多话
梦谈多话 2020-12-31 03:34

jQuery\'s \'live\' method is unable to handle multiple events. Does anyone know of a good workaround to attach multiple events to a function that polls current and future el

相关标签:
4条回答
  • 2020-12-31 04:12

    From jQuery 1.7, "on" function is what you're looking for :

    $("a").on({
        click: function() {
            // do something on click
        },
        mouseenter: function() {
           // do something on mouseenter
        },
        mouseleave: function() {
             // do something on mouseleave
        }
    });
    
    0 讨论(0)
  • 2020-12-31 04:12

    As of jQuery 1.4.1 .live() can accept multiple, space-separated events, similar to the functionality provided in .bind(). For example, we can "live bind" the mouseover and mouseout events at the same time like so:

    $('.hoverme').live('mouseover mouseout', function(event) {
      if (event.type == 'mouseover') {
        // do something on mouseover
      } else {
        // do something on mouseout
      }
    });
    
    0 讨论(0)
  • 2020-12-31 04:15

    Try in this way:

    ("#button").bind("click keyup", function(){
    
    // your code goes here
    
    }) 
    
    0 讨论(0)
  • 2020-12-31 04:27

    In jQuery 1.7 there is an API that allow you to do it easily...

    $(".myClass").on({
        click: function(){
             alert("You click on me!");
        },
        mouseenter: function(){
             alert("Do you want click on me?");
        }
    });
    

    In my opinion this method is completely efficient and gathering all abilities that you can use in element's event....

    Take a look at this page .on() [ jQuery 1.7 API ]

    0 讨论(0)
提交回复
热议问题