常用事件
事件目标和冒泡
- A
- B
- C
- D
自定义事件:
我的理解,就是将某元素的行为进行封装, 这样将事件的触发与行为进行解耦
参考代码:
trigger demo /*An example is probably the best way to explain. Suppose you have a lightbulb * in a room in a house. The lightbulb is currently turned on, and it's controlled by two three-way switches and a clapper: Triggering the clapper or either of the switches will change the state of the lightbulb. The switches and the clapper don't care what state the lightbulb is in; they just want to change the state If there are any lights on in the house, we want the master switch to turn all the lights off; otherwise, we want it to turn all lights on*/房子1开关1开关2房子2开关1开关2总开关
注意事项:
1. 相同事件添加到父节点即可,通过target来获取当前点击的元素
$('#myTable').click(function(e) {var $clicked = $(e.target);$clicked.css('background', 'red'); });
2. 别把过多代码绑定到document.ready中,可以把部分不需要的移入windows.load中
以下是引用片段:
$(window).load(function(){// 页面完全载入后才初始化的jQuery函数.});
3.可以用data(),存储临时变量
以下是引用片段:
$(function(){ $("button").click(function(){ if( $("p").data("flag") ){ $("p").text("true"); $("p").data("flag",false); }else{ $("p").text("false"); $("p").data("flag",true); }});})