jQuery之事件绑定

bind() 方法为被选元素添加一个或多个事件处理程序,并规定事件发生时运行的函数。

下面是具体的用法:

复制代码
<script type="text/javascript">
$(function(){
    $head = $("#panel h5.head");//选中
    $head.bind("click",function(){//如果点击,就会触发事件,将下面的内容显示出来
        $(this).next().show();
    })
})
</script>
复制代码

is(":hidden")可以判断对象是否隐藏。

is(":visible")可以判断对象是否可见。

复制代码
<script type="text/javascript">
$(function(){
    $("#panel h5.head").bind("click",function(){
        var $content = $(this).next();
        if($content.is(":visible")){//如果可见,将其隐藏。如果隐藏,将其可见。
            $content.hide();
        }else{
            $content.show();
        }
    })
})
</script>
复制代码

mouseover鼠标飘过。

复制代码
<script type="text/javascript">
$(function(){
    $("#panel h5.head").bind("mouseover",function(){
        $(this).next().show();//飘过显示
    });
      $("#panel h5.head").bind("mouseout",function(){
         $(this).next().hide();//飘离隐藏
    })
})
</script>
复制代码

直接将事件简写成如下的方式也可以:

复制代码
<script type="text/javascript">
$(function(){
    $("#panel h5.head").mouseover(function(){
        $(this).next().show();
    });
    $("#panel h5.head").mouseout(function(){
         $(this).next().hide();
    })
})
</script>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值