$(“对象”).事件(
function(){
}
);
规定当 DOM 完全加载时要执行的函数
$(document).ready(function(){})
添加/触发 click 事件
$(“p”).click(function(){})
获取焦点
$(“input”).focus(function(){})
失去焦点
$(“input”).blur(function(){})
绑定鼠标事件
$("#div1").hover(function(){},function(){}) hover有2个函数,
绑定键盘事件
$(“input”).keypress(function(){})
绑定事件隐藏
$(“div1”).hide()
绑定事件显示
$(“div2”).show()
设置CSS属性值
$(“this”).css(“属性”,“属性值”);
获取事件文本
$(“span1”).text();、
jQuery的on/off方法为事件绑定/解除绑定提供统一的API;如bind、live、delegate方法只是将调用传递给on方法;同时,jQuery也为一些事件提供了专门的方法
<style>
#cat
{
background:red;
}
#dog
{
background:green;
}
</style>
<script src="jquery.min.js"></script>
<script>
i=0;
$(document).ready(function()
{
$("#cat").click(function(){
$("#dog").hide();
});
$("input").focus(function(){
$(this).css("background-color","red");
});
$("input").blur(function(){
$(this).css("background-color","black");
});
$("input").keypress(function(){
$("p").text(i+=1);
});
$("#cat").focus(function(){
$("#dog").hide();
});
//窗口最大化, 还原
$(window).resize(
function(){
$("span").text(i+=1);
}
);
}
);
</script>
<div id="cat">cat</div>
<div id="dog">dog</div>
<input type="text"></input>
<p >hover有2个函数</p>
<span>显示内容</span>
jQuery事件:jQuery可以触发任意元素的任意事件;jQuery的事件也就是指HTML 中发生某些事件时所调用的方法
总结事件绑定的方法jQuery 事件方法
$(document).ready(function):将函数绑定到文档的就绪事件(当文档完成加载时)
$(selector).click(function):触发或将函数绑定到被选元素的点击事件
$(selector).dblclick(function):触发或将函数绑定到被选元素的双击事件
$(selector).focus(function):触发或将函数绑定到被选元素的获得焦点事件
$(selector).mouseover(function):触发或将函数绑定到被选元素的鼠标悬停事件