سؤال

//<div class="test"></div>
$(".test").append('<div class="single">Items</div>');//some function
$(".single").on({//when in lower version "live" works fine
mouseenter:function(){
console.log("mouseenter");
},
mouseout:function(){
console.log("mouseout");
},
click:function(){
console.log("click");
}
})

but in 2.0.3 "live" is undefined,but "on" seems not to get the event what's wrong,"on" is not more powerfull than "live"? what i suppose to do,thanks

هل كانت مفيدة؟

المحلول

what you need to do is to define a scope for .on like:

//<div class="test"></div>
$(".test").append('<div class="single">Items</div>');//some function
$(".test").on({
mouseenter:function(){
console.log("mouseenter");
},
mouseout:function(){
console.log("mouseout");
},
click:function(){
console.log("click");
}
}, ".single")

نصائح أخرى

The event delegation syntax of on is $(static-ancestor-element).on(event, dynamic-element-selector, handler)

$(".test").append('<div class="single">Items</div>'); //some function
$(document).on({ //when in lower version "live" works fine
    mouseenter: function () {
        console.log("mouseenter");
    },
    mouseout: function () {
        console.log("mouseout");
    },
    click: function () {
        console.log("click");
    }
}, ".single")
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top