一、事件 1 加载DOM $(document).ready(function(){//...}) 另:$(document).ready(function(){//...})的简写方式:$(function(){//...})或$().ready(function(){//...}) 2 事件绑定 $("selector").bind() $("selector").is() (外:方法多次重用可定义局部变量 var $x = $("selector").方法())
$("selector").toggle(fn1,fn2,...,fnN) <script>$(function(){$("panel h5.head").toggle(function(){$(this).next().toggle();},function(){$(this).next().toggle();})})</script>
event.stopPropagation() <script>$('span').bind("click",function(event){var txt = $('msg').html() + "<p>内层span元素被单击</p>";$('#msg').html(txt);event.stopPropagation();})</script>event.preventDefault() <script>$(function(){$("#submit").bind("click",function(event){var username=$("#username").val();if(username==""){$("#msg").html("<p> 文本框的值不能为空</p>");event.preventDefault();}});})</script>return false; (外:事件捕获和事件冒泡是相反的过程,事件捕获是从DOM顶端往下开始触发,jQuery不支持,故本书从略)
<script>$("a").click(function(event){alert(event.type);return false;})</script>上面返回"click" event.target <script>$("a[href=http://google.com]").click(function(){alert(event.target.href);return false;})</script>上面返回"http://google.com" event.relatedTarget event.pageX / event.pageY event.which event.metaKey event.originalEvent
<script>$(function(){$('#btn').bind("click",myFun1=function(){ //先绑定$('#test').append("...");});})</script><script>$('#delOne').click(function(){$('#btn').unbind("click",myFun1); //后删除})</script>$("selector").one()
<script>$("selector").mouseover(function{//...});$("selector2").click(function(){$("selector").trigger("mouseover"); //或者$("selector").mouseover()})</script>自定义事件的例子 <script>$("selector").bind("myClick",function(){//...}); //绑定自定义事件$("selector").trigger("myClick"); //触发自定义事件</script>$("selector").trigger(type[,data]) <script>$("#btn").bind("myClick",function(event,message1,message2){$("#test").append("<p>"+message1+message2+"</p>");});$("#btn").trigger("myClick", ["我的自定义","事件"]);</script>
<script>$(function(){$("div").bind("mouseover mouseout",function(){$(this).toggleClass("over"); //切换class});})</script>$("selector").bind("type.命名空间",function(){//...}) $("selector").trigger("type!")
二、动画
常见的动画例子 <script>//自定义动画的例子$(function(){$("selector").click(function(){$(this).animate({left:"500px"},3000); //selector在3秒内向右移动500px});})</script><script>//累加、累减动画的例子$(function(){$("selector").click(function(){$(this).animate({left:"+=500px"},3000); //连续触发click事件时,在当前位置累加500px});})</script><script>//多重动画的例子$(function(){$("selector").click(function(){$(this).animate({left:"500px",top:"300px",height:"+=100px"},3000); //向右下30度方向运动,同时增加高度});})</script><script>//按顺序执行多个动画的例子$(function(){$("selector").click(function(){$(this).animate({left:"500px"},3000).animate({top:"300px"},3000); //动画队列});})</script>5 动画回调函数 <script>$("selector").click(function(){$(this).animate({property1:"value1"},time).animate({property2:"value2"},time,function(){$(this).css("property3","value3"); //css()方法利用回调函数加入动画队列});})</script>(注:动画回调函数适用于jQuery所有的动画效果方法) 切换动画的例子: <script>$("selector").hover(function(){$(this).stop().animate();},function(){$(this).stop().animate();})</script>clearQueue参数设置为true时,将清空当前元素接下来尚未执行完的动画队列 <script>$("selector").hover(function(){$(this).stop(true).animate().animate() //如此时触发光标移出事件,直接跳过后面的动画队列,避免执行本队列第二个动画},function(){$(this).stop(true).animate().animate()})</script> is(":animated") <script>if(!$("selector").is(":animated")){ //判断元素是否正处于动画状态 //如果当前没有进行动画,则添加新动画}</script> $("selector").toggle() $("selector").slideToggle() $("selector").fadeTo() |
|