Optimize your code by not using live()
as we cannot stop propagation of live()
events
Use on()
(jQuery 1.7+) or delegate()
(below 1.7)
Most efficient solution for your scenario in this case would be:
// $('.mmenu').on("click", ".menu_button", function() { // jQuery 1.7 & up
$('.mmenu').delegate(".menu_button", "click", function() {
var id = $(this).attr('id') // or this.id
if ( id == "m1" ) {
// ..code
}
});
In this way, you have only one click event bound to the main div $('.mmenu')
, which will also work if you add elements (new li with divs) in the future
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…