JavaScript use (vii)

Source: Internet
Author: User

1.JavaScript Scopes

Scopes can access a collection of variables.

1.1 JavaScript Scopes

In JavaScript, objects and functions are also variables.

In JavaScript, scopes are a collection of accessible variables, objects, and functions.

JavaScript function Scope: scope is modified within a function.

1.2JavaScript Local Scope

Variables are declared inside a function, and variables are local scopes.

Local variables: can only be accessed inside the function.

// The carname variable cannot be called here function MyFunction () {    var"Volvo";     // The carname variable can be called within a function }

Because local variables are only used within functions, different functions can use variables of the same name.

Local variables are created when the function starts executing, and local variables are automatically destroyed when the function finishes executing.

1.3JavaScript Global Variables

A variable is defined outside a function, which is a global variable.

Global variables have global scope : All scripts and functions in a Web page can be used.

var " Volvo "   the carname variable function myFunction () {    / // function can be called in Carname variable /c10>}

If a variable is not declared within a function (without using the var keyword), the variable is a global variable.

The following instance is carname within a function, but is a global variable.

// The carname variable can be called here function MyFunction () {    "Volvo";     // The carname variable can be called here }

1.4JavaScript Variable life cycle

The JavaScript variable life cycle is initialized when it declares.

Local variables are destroyed after the function has finished executing.

Global variables are destroyed after the page is closed.

1.5 function parameters

function parameters only work within a function, which is a local variable.

Global variables in 1.6HTML

In HTML, a global variable is a Window object: All data variables belong to the Window object.

// Here you can use the Window.carname function MyFunction () {    "Volvo";}

2.JavaScript Events

HTML events are things that happen on HTML elements.

JavaScript can trigger these events when JavaScript is used in HTML pages.

2.1HTML Events

HTML events can be browser behavior or user behavior.

The following is an example of an HTML event:

    • HTML Page finished loading
    • When the HTML input field changes
    • HTML button is clicked

Usually, when an event occurs, you can do something about it.

JavaScript can execute some code when an event is triggered.

You can add event properties to HTML elements and use JavaScript code to add HTML elements.

Single quote: <some-html-element some-Event='JavaScript code '> double quotes:< Some-html-element some-Event="JavaScript code ">

In the following instance, the onclick attribute (plus code) is added to the button element:

<button onclick="getElementById (' demo '). Innerhtml=date ()"> What time Is it now? </ Button>

In the above example, the JavaScript code modifies the contents of the id= "demo" element.

In the next instance, the code modifies the contents of its own elements ( using this. InnerHTML):

<button onclick="this.innerhtml=date ()"> Now the time is?</button>

JavaScript code is usually a few lines of code. It is more common to invoke the event properties:

<button onclick="displaydate ()"> Now the time is?</button>

2.2 Common HTML Events

A list of some common HTML events: onchange, onclick, onmouseover, onmouseout, onkeydown, onload

What can 2.3JavaScript do?

Events can be used to process form validation, user input, user behavior, and browser actions:

    • Trigger event when page loads
    • Trigger event when page closes
    • The user clicks the button to perform the action
    • Verifying the legality of user input
    • Wait a minute...

You can use several methods to execute JavaScript event code:

    • HTML Event Properties can execute JavaScript code directly
    • HTML Event Properties can call JavaScript functions
    • You can specify your own event handlers for HTML elements
    • You can stop the event from happening.
    • Wait a minute...

JavaScript use (vii)

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.