Home > Article > Web Front-end > Detailed description of jquery events and binding events
Jquery events and binding events are both very important knowledge. This article mainly introduces the relevant knowledge of jquery events and binding events, which has a good reference value. Let's take a look with the editor below, I hope it can help everyone.
1. First, let’s take a look at the commonly used ways to add events:
<input type="button" id="btn" value="click me!" onclick="shao();" /> <script type="text/javascript"> function shao() { alert("msg is showing!"); } </script>
The most commonly used way for us to add events is to add onclick element attributes to elements
The disadvantage of this method is:
can only be used for one event processing function. In the event processing function method, the way to obtain the event object is different.
Events in jQuery
ready event:
When the page is loaded, execute the function:
<script> $(document).ready(function(e){ alert(document.getElementById("aa").innerHTML); //若是要写function方法,不可以在里面写 }) //要在外面写 </script>
This method can be called wherever it is written;
Mouse event :
<script> $("#aa").click(function(){ alert("点击事件"); }) $("#aa").dblclick(function(){ alert("双击事件"); }) $("#aa").mouseover(function(){ alert("鼠标移上") }); $("#aa").mouseout(function(){ alert("鼠标离开"); }) $("#aa").mousemove(function(){ alert("鼠标移动"); }) $("#aa").mouseup(function(){ alert("鼠标抬起"); }) $("#aa").mousedown(function(){ alert("鼠标按下"); }) 键盘按键按下:给id加没有作用,需要给整个页面加所以用$(document) $(document).KeyEvent(function(){ alert("鼠标离开"); }) </script>
Form element events:
<script> $("#shao").focus(function(){ alert("获得焦点"); }) $("#shao").blur(function(){ alert("失去焦点"); }) $("#shao").change(function(){ alert("值发生变化,change事件"); }) $("#shao").keydown(function(){ alert("键盘按下"); }) </script>
2. Binding events (hanging events):
Events that can dynamically change buttons;
What Is it dynamic binding?
Dynamic binding refers to dynamically added DOM nodes or html elements, which do not exist when they are initially run. If you want to add events to these dynamically added nodes, you must use jquery's on method to bind the events.
bind() adds one or more event handlers to the matching element.
Usage:
$(selector).bind(event,data,function)
Note: The bind() function can only handle events for existing elements. Set up the
code: first write two buttons:
<body> <p id="aa" style="width: 100px; height: 100px; background-color: blueviolet;">hello</p> <!--<input type="text" id="shao" />--> <input type="button" id="btn1" value="挂事件" /> <input type="button" id="btn2" value="移除事件" /> </body>
First operate the click and hang event:
<script> //挂事件, $("#btn1").click(function(){ //点击挂事件,给p绑定一个事件: $("#aa").bind("click",function(){ //bind绑定事件 alert("点击"); }); //括号里两个参数,第一个是事件类型(事件名称),第二个参数是要执行的代码 }) </script>
In this case, click and hang the event:
Remove event button:
<script> //移除事件; $("#aa").click(function(){ //点击移除事件;把p里面的事件移除掉 $("#aa").unbind("click"); //unbind移除绑定,填一个参数,要移除哪个事件 }) </script>
Click to remove, click the event to cancel aa
3. Event data
General events include events Source and time data:
Event data: At this time, the data will be passed
js simplified, you don’t need to write the event source, because you can get
4. JSON syntax:
JSON structure:
Json is simply an object and an array in JavaScript, so these two structures are objects and arrays, which can be represented by these two structures various complex structures.
(1) Object: The object is represented in js as the content enclosed by "{}", and the data structure is the key-value pair structure of {key: value, key: value,...}, in In object-oriented languages, key is the attribute of the object, and value is the corresponding attribute value, so it is easy to understand. The value method is object.key to obtain the attribute value. The type of this attribute value can be a number, a string, an array, or an object. Several kinds.
(2) Array: The array in js is the content enclosed by square brackets "[]". The data structure is ["java", "javascript", "vb",...], and the value is The method is the same as in all languages, using index acquisition. The type of field value can be numbers, strings, arrays, or objects.
Complex data structures can be combined through the two structures of objects and arrays.
json is a lightweight data exchange format
Quanpin:
JavaScript Object Notation
Definition syntax:
var j = { "one":"111111", "two":"22222" };
Value method:
Index:
//数组的取值方式: alert(j["one"]);//直接取索引的方法
Dot syntax:
//点语法: alert(j.one);
JSON can also be a two-dimensional array:
var j = { "one":"111111", "two":"22222", "three":{"aa":"33333"}, }; //数组的取值方式: //alert(j["one"]);//直接取索引的方法 //点语法: alert(j.one); alert(j.three.aa);
Traverse JSON data:
//遍历 for(var v in j) { //定义一个变量v,把j拿到v里面,关键字不是”as“了,是”in“, alert(v); // 这样便利的是索引 alert(j[v]); // 这样是根据索引来取值 }
json does not have a length attribute, so the for loop is not suitable for json
But for-in is also suitable for arrays
Related recommendations;
Detailed examples of the properties of jQuery event objects
The difference between mouseover and mouseenter in jQuery events
When using on to bind events in jQuery What needs attention
The above is the detailed content of Detailed description of jquery events and binding events. For more information, please follow other related articles on the PHP Chinese website!