如何使用替换live(jQuery v2.0.3)

时间:2013-12-01 10:19:06

标签: jquery live

//<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");
}
})

但在2.0.3中“live”未定义,但“on”似乎没有得到该事件 怎么了,“on”并不比“live”更强大? 我想做什么,谢谢

2 个答案:

答案 0 :(得分:1)

您需要做的是为.on定义范围,如:

//<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")

答案 1 :(得分:0)

on的事件委托语法为$(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")