06jquery-05-Events

Source: Internet
Author: User

The code for different browser binding events is not the same, so we can block differences between browsers by using jquery to write code.
In jquery, you can use the onTo bind an event, specifying the name of the event and the corresponding handler function:
  
 
  1. // 获取超链接的jQuery对象:
  2. var a = $(‘#test-link‘);
  3. a.on(‘click‘, function () {
  4. alert(‘Hello!‘);
  5. });

But generally, we don't use this because we can directly use encapsulated event methods, such as the on click above, which we can write directly:
 
   
  
  1. a.click(function () {
  2. alert(‘Hello!‘);
  3. });

1. Common Event 1.1 mouse events
Click Trigger when mouse clicks
DblClick Trigger when mouse double-click
MouseEnter Trigger when Mouse enters
MouseLeave Trigger when mouse moves out
MouseMove Triggers when the mouse moves inside the DOM
Hover Two functions are triggered when the mouse enters and exits, equivalent to MouseEnter plus MouseLeave

1.2 Keyboard event keyboard events only work on the DOM of the current focus, usually <input> and <textarea>.
KeyDown Triggered when the keyboard is pressed
KeyUp Trigger when keyboard is released
KeyPress Triggers after pressing the key once

1.3 Other events
Focus Triggered when the DOM gets focus
Blur Triggered when the DOM loses focus
Change triggered when the content of <input>, <select>, or <textarea> changes
Submit triggered when <form> commits
Ready Triggered when the page is loaded and the DOM tree finishes initializing

Note: Ready is used only for document objects, our JS code is usually waiting for the DOM to be loaded and then executed, or DOM is often not found, so our own initialization code needs to be placed in the ready event, ensuring that the DOM has been initialized:
  
 
  1. <html>
  2. <head>
  3. <script>
  4. $(document).on(‘ready‘, function () {
  5. $(‘#testForm).on(‘submit‘, function () {
  6. alert(‘submit!‘);
  7. });
  8. });
  9. </script>
  10. </head>
  11. <body>
  12. <form id="testForm">
  13. ...
  14. </form>
  15. </body>

Or:
  
 
  1. $(document).ready(function () {
  2. // on(‘submit‘, function)也可以简化:
  3. $(‘#testForm).submit(function () {
  4. alert(‘submit!‘);
  5. });
  6. });

Usually we use the following method, which is more concise:
 
   
  
  1. $(function () {
  2. // init...
  3. });

2, event parameters some events such as MouseMove and keypress, we need to get the position of the mouse and the value of the key. All events will pass inEventObject as a parameter, you can get more information from the object:
  
 
  1. $(function () {
  2. $(‘#testMouseMoveDiv‘).mousemove(function (e) {
  3. $(‘#testMouseMoveSpan‘).text(‘pageX = ‘ + e.pageX + ‘, pageY = ‘ + e.pageY);
  4. });
  5. });
As shown, the x, Y coordinate values of the mouse movement are constantly changing: 3. Unbind an event that has been bound to unbind it byoff (' xxx ', function)Implementation, it is worth noting that the off-mode unbind is unable to cancel the direct event method, and the following unbind is not valid:
  
 
  1. // 绑定事件:
  2. a.click(function () {
  3. alert(‘hello!‘);
  4. });
  5. // 解除绑定:
  6. a.off(‘click‘, function () {
  7. alert(‘hello!‘);
  8. });

This is a two different function object, so off cannot cancel the first anonymous function that has been banding.
You can use off (' click ') to remove all functions of the bound click event, or Off () to remove all types of event handlers that have been bound.
4. Event Trigger conditions Remember,The triggering of an event is always caused by a user action, that is, if you useJS code to change the value, does not trigger the corresponding event!
If you must use the JS trigger event, you can directly invoke the non-parametric event method to triggerFor example:
 
   
  
  1. var input = $(‘#test-input‘);
  2. input.val(‘change it!‘);
  3. input.change(); // 触发change事件
  4. // input.change()相当于input.trigger(‘change‘),它是trigger()方法的简写

In addition, some JS code even if set trigger, also because the browser security settings can not be implemented, can only be triggered by the user.

06jquery-05-Events

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.