jQuery之合成事件

jQuery提供一些方法将两种效果合并到一起,比如:mouseover、mouseout。

下面是一些案例:

hover(over,out)一个模仿悬停事件(鼠标移动到一个对象上面及移出这个对象)的方法。这是一个自定义的方法,它为频繁使用的任务提供了一种“保持在其中”的状态。
参数 :
over (Function) : 鼠标移到元素上要触发的函数。
out (Function): 鼠标移出元素要触发的函数。

<script type="text/javascript">
$(function(){
    $("#panel h5.head").hover(function(){
        $(this).next().show();//鼠标悬浮时触发
    },function(){
        $(this).next().hide();//鼠标离开时触发
    })
})
</script>

toggle(fn,fn)
每次点击时切换要调用的函数。
如果点击了一个匹配的元素,则触发指定的第一个函数,当再次点击同一元素时,则触发指定的第二个函数。随后的每次点击都重复对这两个函数的轮番调用。
可以使用unbind("click")来删除。

<script type="text/javascript">
$(function(){
    $("#panel h5.head").toggle(function(){
            $(this).next().show();//第一次点击时触发
    },function(){
            $(this).next().hide();//第二次点击时触发,之后不停的切换
    })
})
</script>

toggle() 方法切换元素的可见状态。
如果被选元素可见,则隐藏这些元素,如果被选元素隐藏,则显示这些元素。toggle() 方法切换元素的可见状态。
如果被选元素可见,则隐藏这些元素,如果被选元素隐藏,则显示这些元素。

所以上述的代码还可以写成:

<script type="text/javascript">

$(function(){
    $("#panel h5.head").toggle(function(){
            $(this).next().toggle();
    },function(){
            $(this).next().toggle();
    })
})

/*$(function(){
    $("#panel h5.head").click(function(){
            $(this).next().toggle();
    })
})*/
</script>

还可以添加一些css样式:

<style type="text/css">
.highlight{ background:#FF3300; }
</style>
<script type="text/javascript">
$(function(){
    $("#panel h5.head").toggle(function(){//配合css样式使用
            $(this).addClass("highlight");
            $(this).next().show();
    },function(){
            $(this).removeClass("highlight");
            $(this).next().hide();
    });
})
</script>
posted @ 2012-09-10 11:56  TBHacker  阅读(2866)  评论(0编辑  收藏  举报