jQuery中的on和off方法
on 方法
语法如下:
$(selector).on(event,childSelector,data,function(eventObj))
字段说明
字段 | 使用说明 |
---|---|
selector | jQuery选择器,如’#id1’,指示事件绑定在哪个元素上 |
event | 事件名称,如‘mousedown’(按下鼠标),指示触发功能所需执行的事件 |
childSelector | jQuery选择器,如’#ids’,指示事件需要发生在哪个元素上 |
data | 在执行函数功能时向函数传递的参数,以字典形式传递,如‘{key1:‘value1’, key2:‘value2’}’,需通过event.data.key的方式调用,参见代码用例。 |
function | 发生事件时执行的函数 |
也可通过map对象向on方法传参(结构类似于只有一个键值对的字典),同时定义多个事件,如下代码所示:
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("p").on({
mouseover:function(){
$("body").css("background-color","lightgray");},
mouseout:function(){
$("body").css("background-color","lightblue");},
click:function(){
$("body").css("background-color","yellow");}
});
});
</script>
</head>
<body>
<p>Click or move the mouse pointer over this paragraph.</p>
</body>
</html>
(以上代码转自