Course 5 of jQuery (events and event objects in jquery)

Source: Internet
Author: User

Upload events and event objects in jquery

1. Load document events

2. mouse events

3. Keyboard Events

4. Form Events

5. browser events

6. event object

7. Event binding method

8. How to remove an event

9. Event bubbling (omitted)

10. Event namespace




Load document events

A. ready () omitted

B. $. holdReady (bool) pause or resume the ready event

C. load (), unload () 1.8 removed




Mouse events

Click () click the left button event

Dblclick () double-click event

Focusin () obtains the focus event and can act on the parent level.

Focusout () loss of focus event, can act on the parent level

Mousedown () the mouse-down event is different from click ().

Mouseup () mouse up event

Mousemove (), move the mouse

Mouseover () mouse Entry Event

Mouseout () mouse exit event

Mouseenter () mouse Entry Event

Mouseleave () mouse exit event

Hover () binds both mouseenter and mouseleave events

<Body>
<P> This is a P tag. </p>

<Div>
<Input type = "text" name = "" id = "" value = "123456"/>
<Span style = "display: none"> obtained focus </span>
<! -- <P> <input type = "text" value = "654321"/> </p>
<P> <em> <input type = "text" value = "abcdef"/> </em> </p> -->
</Div>
<Script>

// When the left mouse button is pressed, then the left mouse button is raised to complete the click event.
/* $ ('P'). click (function (){
Alert ($ (this). text ());
});*/

/* $ ('P'). dblclick (function (){
Alert ($ (this). text ());
});*/

/* $ ('Div '). focusin (function (){
$ (This). children ('span '). show ();
});*/

/* $ ('Input'). focusin (function (){
$ ('Span '). show ();
});*/

/* $ ('P'). mousedown (function (){
Alert ($ (this). text ());
});*/

/* $ ('P'). mouseup (function (){
Alert ($ (this). text ());
});*/

/* $ (Document). mousemove (function (e ){
Var x = e. pageX;
Var y = e. pageY;
$ ('Input'). val (x + ',' + y );
});*/

/* $ ('P'). mouseover (function (){
Watermark (this).css ('background', 'red ');
}). Mouseout (function (){
Watermark (this).css ('background', 'None ');
});*/

$ ('P'). mouseenter (function (){
Watermark (this).css ('background', 'red ');
});

$ (Document). mouseleave (function (){
Certificate ('p'background .css ('background ', 'None ');
});

</Script>
</Body>

------------------------------------------------- Hover ------------------------------------

<Body>
<P> P tag </p>
<Script>
// Hover is an event that simulates mouse entry and mouse exit
// Hover (fn1, fn2 );
$ ('P'). hover (function (){
Watermark (this).css ('background', 'red ');
}, Function (){
Watermark (this).css ('background', 'None ');
});
</Script>
</Body>

Keyboard Events

A. keydown () keyboard press event

B. Weak keyup () Keyboard Events

C. keypress () is similar to keydown (), but there is a difference

<Body>
<Input type = "text" value = "text"/>
<Script>
$ ('Input'). keydown (function (){
Alert ($ (this). val ());
});

/* $ ('Input'). keyup (function (){
Alert ($ (this). val ());
});*/

/* $ ('Input'). keypress (function (){
Alert ($ (this). val ());
});*/
</Script>
</Body>



Form Events

Focus () Get focus events

Blur () loss of focus events

Change () form value change event

Select () event when a form element is selected, which can only be used for Input [text] And textarea

Submit () Form submission event

<Body>
<! -- <Input type = "text" name = "" id = "" value = "11111" type = "codeph" text = "codeph"/> -->
<Input type = "password" name = "" id = "" value = "11111"/>
<! -- <Input type = "file" name = "" id = "/> -->
<Textarea name = "" id = "" cols = "30" rows = "10"> 1231231212213 </textarea>
<Span style = "display: none"> asdasdfsdf </span>
<Script>
/* $ ('Input'). focus (function (){
$ ('Span '). show ();
});

$ ('Input'). blur (function (){
$ ('Span '). hide ();
});*/

// When the value in the element with the focus event changes and the blur event is triggered, a change event is completed.
/* $ ('Input [type = file] '). change (function (){
$ ('Span '). show ();
});*/

/* $ ('Input'). select (function (){
$ ('Span '). show ();
});*/

/* $ ('Textea '). select (function (){
$ ('Span '). show ();
});*/
</Script>
</Body>



Browser events

A. resize () browser window size change event

B. events that occur when the scroll () browser scroll bar is moved

C. error () 1.8 discarded

<Body style = "height: 3000px">
<Script>
/* $ (Window). resize (function (){
Alert ('browser window changed! ');
});*/

$ (Window). scroll (function (){
$ ('Body'). append ('<div> offset of the scroll bar position </div> ');
});
</Script>
</Body>

Event object

Event. pageX get the X axis coordinate of the mouse relative to the document

Event. pageY get the Y axis coordinate of the mouse relative to the document

Event. preventDefault () blocks default browser behavior

Event. stopPropagation () prevents bubbling

Event. which listens for keyboard input and mouse operations

Others

<Body style = "height: 3000px; width: 3000px">
<Input type = "text" style = "position: fixed; top: 10px; left: 10px"/>
<Script>
$ (Document). click (function (e ){
Var x = e. pageX;
Var y = e. pageY;
$ ('Input'). val (x + ',' + y );
});
</Script>
</Body>

<Body>
<Form action = "http://www.zixue.it" id = "form1">
<Input type = "text" value = "11111" name = "username"/>
<Input type = "password" value = "22222" name = "password"/>
<Input type = "submit" value = "submit"/>
</Form>
<Script>
$ ('# Form1'). submit (function (e ){
Alert ('submission successful !! ');
E. preventDefault ();
});
</Script>
</Body>

How to bind and remove events

Bind () binding event

Unbind () Remove event

On () binding event

Off () Remove event

One () executes an event and then destroys it.

Although delegate () has not been abandoned, it is officially recommended to use on () instead.

Undelegate () is replaced by off

<Body style = "height: 3000px">
<P> This is a P tag. </p>
<Script>
/* $ ('P'). click (function (){
Alert ($ (this). text ());
});*/

/* $ ('P'). bind ('click', function (){
Alert ($ (this). text ());
});
$ ('P'). mouseover (function (){
Watermark (this).css ('background', 'red ');
});

$ ('P'). unbind ('click mouseover ');*/

/* $ ('P'). one ('click', function (){
Alert ($ (this). text ());
});*/

/* $ ('Body'). delegate ('P', 'click', function (){
$ (This). append ('<p> I am a newly added P tag <p> ');
});*/

$ ('Body'). on ('click', 'P', function (){
$ (This). append ('<p> I am a newly added P tag <p> ');
});

</Script>
</Body>


Event namespace

<Body>
<P> I am a P tag </p>
<Script>
// The first developer adds a click event to the p tag so that its background color turns red.
/* $ ('P'). click (function (){
Watermark (this).css ('background', 'red ');
});*/

// The second developer adds a click event to the P tag so that its text color turns blue
/* $ ('P'). click (function (){
Watermark (this).css ('color', 'blue ');
});*/

// The third developer does not want the P tag to add a red background when it is clicked, but only wants to change the text color in the P tag to blue, then he used the unbind method to remove the click event on the P tag.
/* $ ('P'). unbind ('click ');*/

$ ('P'). bind ('click. background ', function (){
Watermark (this).css ('background', 'red ');
});

$ ('P'). bind ('click. color', function (){
Watermark (this).css ('color', 'blue ');
});

$ ('P'). unbind ('click. color ');


</Script>
</Body>

Jquery

Event addendum in

Trigger () event to simulate event occurrence

TriggerHandler () events, simulate events, and prevent bubbles

Live (), die () has been abandoned

The toggle () event has been abandoned.

Event binding Optimization

----------------------- Trigger ----------------------------------

<Body>
<A href = "###"> click </a>
<Script>
$ ('A'). click (function (){
Alert ('I was click ');
});
$ ('A'). mouseover (function (){
Alert ('move the mouse ');
});
$ ('A'). trigger ('mouseover ');
</Script>
</Body>

----------------- TriggerHandler --------------------------

<Body>
<P>
<A href = "###"> click </a>
</P>
<Script>
$ ('A'). click (function (){
Alert ('a element clicked ');
});

$ ('P'). click (function (){
Alert ('P element clicked ');
});

// $ ('A'). trigger ('click ');
$ ('A'). triggerHandler ('click ');

</Script>
</Body>




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.