bind()方法

时间:2023-03-09 03:59:21
bind()方法

当点击鼠标时,隐藏或显示 p 元素:

$("button").bind("click",function(){
$("p").slideToggle();
});

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

$(selector).bind({event:function, event:function, ...})
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script>
$(document).ready(function(){ $("button").bind({
click:function(){$("p").slideToggle();},
mouseover:function(){$("body").css("background-color","red");},
mouseout:function(){$("body").css("background-color","blue");}
});
});
</script>
</head>
<body>
<p>This is a paragraph.</p> <button type="button">请点击这里</button>
</body> </html>

bind()的用法:$("元素").bind({      事件:function(){ $("元素").function(){};},

                事件:function(){$().function(){};},

})

首先一个外层的大轮廓就是jquery的通用格式$("元素").bind({});

然后里面一层就是方法事件及方法可以叠加,还是$(0.function(){}格式: 事件:$("元素").function(){},

最后里面一层还是jquery的基础格式:$("元素").函数(){}

所以套起来就是:

$("元素").bind({事件:$("元素").function(){$("元素").函数(){};},

              事件:$("元素").function(){$("元素").函数(){};}                 

});

.