jQuery 事件注册
- jQuery单个事件注册,
- on() 可绑定一个或多个事件
- on() 的注册事的 优势
div{width:100px;height:100px;background-color:#8A2BE2;}.curr{background-color:#D2691E;}
<div></div>
jQuery -代码
//jQuery 事件注册
// 1、单个事件注册
$("div").mouseover(function() {
$(this).css("width", 300)
});
$("div").mouseout(function() {
$(this).css("width", 100)
});
//2、事件处理 on() 可绑定一个或多个事件
$("div").on({
mouseover: function() {
$(this).css("width", 300)
},
mouseout: function() {
$(this).css("width", 100)
},
click: function() {
$(this).css("width", 500)
}
});
$("div").on("mouseenter mouseleave", function() {
$(this).toggleClass("curr")
})
// 事件委托
$("ul").on("click", "li", function() { //click 是绑定在ul身上,但触发的对像是li
alert(11)
})
//可以给动态创建的元素绑定事件
var di = $("<div>我是新建的</div>");
$("div").append(di)
$("div").on("mouseover", "div", function() {
alert(12522)
})