Basic Javascript knowledge (2) Events and basic javascript knowledge

Source: Internet
Author: User

Basic Javascript knowledge (2) Events and basic javascript knowledge

Event object: (the event object is the property of the window object. when an event occurs, an event object is generated, the Event ends, and the event object disappears)

In IE: window. event; // get the object

In DOM: argument [0]; // get the object

Common attribute methods for Event objects in IE:

1. clientX: X coordinates of the mouse pointer in the client area (excluding the toolbar and scroll bar) when an event occurs;

2. clientY: Y coordinates of the mouse pointer in the client area (excluding the toolbar and scroll bar) when an event occurs;

3. keyCode: For a keyCode event, it indicates the Unicode Character of the key to be pressed. For a keydown/keyup event, it indicates that the keyboard to be pressed is a number (to obtain the value of the key );

4. offsetX: X coordinate of the mouse pointer relative to the object that triggered the event;

5. offsetY: Y coordinate of the mouse pointer relative to the object that triggered the event;

6. srcElement: the element that causes the event;

Common attribute methods for event objects in DOM:

1. clientX;

2. clientY;

3. pageX; X coordinate of the mouse pointer to the page;

4. pageY; Y coordinate of the mouse pointer to the page;

5. StopPropagation: calling this method can prevent further event propagation (bubbling );

6. target: The Event element/object that is triggered;

7. type: event name;

Similarities and differences between the two event objects:

Similarities:

1. Obtain the event type;

2. Get the keyboard code (keydown/keyup event );

3. Check Shift, Alt, Ctrl;

4. Obtain the coordinates of the customer zone;

5. Obtain screen coordinates;

Differences:

1. Obtain the target;

// IE: var oTarget = oEvent. srcElement;

// DOM: var oTarget=oEvent.tar get;

2. Get the verification code;

// IE: var iCharCode = oEvent. keyCode;

// DOM: var iCharCode = oEvent. charCode;

3. prevent default events;

// IE: oEvent. returnValue = false;

// DOM: oEvent. preventDefault;

4. Terminate bubbling:

// IE: oEvent. cancelBubble = true;

// DOM: oEvent. stopPropagation

Event Type:

I. mouse events:

Onmouseover: When the mouse moves in;

Onmouseout: When the mouse is removed;

Onmousedown: When you press the mouse;

Onmouseup: When the mouse is raised;

Onclick: Click the left mouse button;

Ondblclick: double-click the left mouse button;

Ii. Keyboard Events:

Onkeydown: This occurs when you press a key on the keyboard;

Onkeyup: This occurs when the user releases a pressed key;

Keypress: when the user keeps pressing the key;

Iii. HTML events:

Onload: when loading the page;

Onunload: When the page is uninstalled;

Abort: if the user has not been fully reproduced before terminating the loading process, an abort event occurs.

Error: events generated when an error occurs in javascript;

Select: select event triggered when the user selects a character in an input or textarea

Change: In an input or textarea, when it loses focus, the change event is triggered in select.

Submit: triggers the submit event when the form is submitted;

Reset: reset

Resize: The event triggered when the window or frame size is adjusted;

Scroll: events triggered when a user scrolls or has a scroll bar;

Focus: when the focus is lost;

Blur: Get focus;

Javascript event model

1. javascript event model: 1. bubble type: <input type = "button"> when a user clicks the button: input-body-html-document-window (from bottom to top) the IE browser only uses bubble

2. Capture type: <input type = "button"> when you click the button: window-document-html-body-input (top-down)

After ECMA standardization, other browsers support two types. Capture occurs first.

2. Three traditional event writing methods:

1. <input type = "button" onclick = "alert ('helloworld! ') ">

2. <input type = "button onclick = name1 ()" >=====< script> function name1 () {alert ('helloword! ') ;}</Script> // famous Function

3. <input type = "button" id = "input1"> // anonymous Function
Copy codeThe Code is as follows:
<Script>
Var button1 = document. getElementById ("input1 ");
Button1.onclick = funtion (){
Alert ('helloword! ')
}
</Script>

3. Modern event writing methods:
Copy codeThe Code is as follows:
<Input type = "button" id = "input1"> // Add an event to IE

<Script>
Var fnclick (){
Alert ("I Have Been clicked ")
}
Var Oinput = document. getElementById ("input1 ");
Oinput. attachEvent ("onclick", fnclick );
--------------------------------------
Oinput. detachEvent ("onclick", fnclick); // delete an event in IE
</Script>

Copy codeThe Code is as follows:
<Input type = "button" id = "input1"> // Add an event to the DOM

<Script>
Var fnclick (){
Alert ("I Have Been clicked ")
}
Var Oinput = document. getElementById ("input1 ");
Oinput. addEventListener ("onclick", fnclick, true );
--------------------------------------
Oinput. removeEventListener ("onclick", fnclick); // delete an event in the DOM
</Script>

Copy codeThe Code is as follows:
<Input type = "button" id = "input1"> // compatible with IE and DOM add events

<Script>
Var fnclick1 = function () {alert ("I Have Been clicked ")}
Var fnclick2 = function () {alert ("I Have Been clicked ")}
Var Oinput = document. getElementById ("input1 ");
If (document. attachEvent ){
Oinput. attachEvent ("onclick", fnclick1)
Oinput. attachEvent ("onclick", fnclick2)
}
Else (document. addEventListener ){
Oinput. addEventListener ("click", fnclick1, true)
Oinput. addEventListener ("click", fnclick2, true)
}
</Script>


Basic javascript knowledge

You should understand JAVA well ~~

Objects can be hierarchical ~~~

Public class test {
Public string a = "";
}
Test t1 = new test ();
Test1.a = "B ";

It means something similar to the above ~~

Document is implemented by DOM ~~ What I said upstairs is wrong. javascript does not provide DOM and BOM objects. The implementation of these objects is provided by the browser ~~ In IE... you can use vbscript to implement document... Of course there are other languages to implement ..

All is a unique attribute of IE. It seems like it is. I'm not sure ~~ It means to return the collection of all the DOM in the document...
Form. numeric is the numeric object under the form object ~ This may be the case.
<Form id = "form"> <input type = "text" id = "numeric"/> </form>

The recommended book is <JavaScript advanced programming> ~ Is the Basic book ~~

If you do not want to buy books ~~ You can refer to the following two manuals... JScript manuals (JScript Language for MS, refer to 99%) and DHTML manuals (also for MS .. For DOM )~

Basic javascript Problems

Var bbb = aaa (); // return value when bbb is assigned aaa
====================
Var bbb = aaa;
// Bbb is assigned aaa by the address. Because aaa is a "function object", bbb is also a function object.
You can call bbb (). Here, the "function object" has nothing to do with the weak type. This is the language design that uses the function as the First-class function.

Javascript has no "reference" or "pointer" strictly ".
The concept of applying other languages is more metaphysical.
======================================

Xxx = getElementById ("idname"); // returns a DOM element object whose id is idname.
Xxx. onchange = func; // rewrite the onchange method of the element Object (I think of English override, which used the wrong Chinese translation ), the syntax here is to assign a value to the func function object.
Javascript emphasizes the concept of function objects. Because a function is the most basic object and a first-class citizen.
Function foo () {}// create a function
Equivalent
Var foo = new Function () {}// create a Function object
So
Xxx. onchange = foo
Xxx. onchange = new Function ("");
Xxx. onchange = function (){}
All point onchange to another function (object)
======================

To the pervasive francis674
Your answer is too wrong.

"Var a = funcitn (){}"
"A is a variable, the type is function, and alert (a) is the text definition of the function body"
Not only is spelling wrong, but it cannot be explained, so I had to say the content in the metaphysics.
I have never mentioned
Var a = function (){}
You can tell the subject that this is to define an unknown function in the form of function declaration.
Var a = new Function ("") is used to define Function objects.
These two types are interconnected.

"Xxx. onchange = func this is an event Delegate "// It is not an event Delegate. You only need to re-specify the object. When the object is not placed in the event model, you only need to re-specify the object in syntax and override the override object method.

"The essential difference is that it can be understood as a variable, a pointer, and actually a variable ."
Advanced languages have never been pointers, and blind analogy is metaphysics.

"Javascript is a weak scripting language. Both functions and variables are called variables. "// Can a function become a variable?
"Javascript does not support oop and other features" // does js not support oop or js? Js is the object-oriented programming language of the prototype model and supports oop very much.
These are all ridiculous and wrong. Why is it metaphysics? I learned some usage and hypothetical principles, mixed with many misunderstandings.
The method can only be obtained, but it can only be obtained. If you want to express all these mistakes to beginners? What can I learn from you? Shouldn't blame others for recognizing these mistakes .... Remaining full text>

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.