JS tutorial-javascript syntax Introduction

Source: Internet
Author: User
Tags types of functions
JavaScript is a Web scripting language developed by Netscape Based on Client browsers, oriented (based on) objects, and event-driven.

Php tutorial-JavaScript programming

I. Introduction

Dynamic languages such as PHP, ASP, and JSP are all parsed and executed by the server.

Static HTML, CSS, and JAVASCRIPT are parsed and executed by the client browser.

1. Introduction

  • JavaScript was first developed by Netscape Based on Client browsers, oriented (based on) objects, and event-driven Web Page scripting language.

2. Main Functions

  • Interactive operations
  • Form Verification
  • Webpage Special Effects
  • Web games
  • Server Script Development

Ii. javascript syntax

1. Basic syntax

  • JavaScript is case sensitive. variables a and A are two variables.
  • JavaScript scripts must be embedded in HTML files. Html files --- à js scripts. Js itself cannot be directly run and must be implemented through html
  • JavaScript scripts cannot contain HTML markup code.

Script

Html Tag error is not allowed:

Script

  • A script statement is recommended for each row: A = 10; B = 20;
  • Extra points can be recommended at the end of the statement.

A = 10

A = 10;

  • JavaScript scripts can be stored as an external file independently.

Js script ----> In the. js File

. Html file ---> js external file

2. script tag

  • Tags: script, script
  • Attribute:

Language: script language

Src: loading location

3. Variables in javascript

  • Variables are containers used to temporarily store values. The values stored in variables can be changed.
  • A variable must be declared before it can be used. If the variable is not declared, it is a global variable.
  • Naming rules for variables: the first character must be an English letter or an underscore (_). The subsequent character can be an English letter, number, or underscore. The variable name cannot be a reserved word in JavaScript.
  • Scope of variables: global variables and local variables

4. Javascript Data Types

  • String
  • Number
  • Boolean
  • Undefined
  • Null
  • Object

5. javascript Operators

  • Arithmetic Operator: +-*/%

++ // Note that the front + + back ++

-- // Note before -- after --

  • Comparison OPERATOR: >>=<=! ======! =
  • Logical OPERATOR: & |!
  • Assignment operator: = + =-= * =/= % =
  • String OPERATOR: ++ =

Iv. javascript Process Structure

  • Sequential Structure
  • Branch Structure: If else if else switch
  • Loop Structure

For while do .... While ...... In

// While: first judge and then execute

// Do... while: first execute and then judge

// Do... while at least one while loop execution is unknown

// For is generally used to know the number of cycles

// While is used for the number of unknown Loops

Skip loop:

  • Continue: jump out of this loop
  • Break: jump out of the entire Loop

Two small jobs:

Question 1: There is a thing. I don't know its number. I don't know what it is. I have two to three. I have three to five to three. I have two to seven.

Question 2: use javascript to output the 9-9 multiplication table

(For the answer, see the last page of this Article)

# P # php tutorial-js tutorial-common js functions # e #

Iv. Javascript Functions

Benefits of functions: code reuse and modular programming

There are two types of functions: system functions, custom functions.

1. User-Defined Functions

  • You must define a function before calling it.
  • The function definition includes the function name, parameter list, and function body.
  • Define the Function Format

Function Name ([parameter 1, parameter 2...]) {

Function execution part;

Return expression;

}

2. arguments

  • Arguments is a list of function parameter arrays.
  • When using the arguments attribute, you do not need to specify the parameter list of the function when defining the function. You can enter the parameter list of the function when calling the function.
  • Implement multiple functions of a function

V. Information Box

  • Alert ("str") message box
  • Confirm (str) confirmation box

The Confirm method returns a Boolean value. If the user clicks "yes", the return value is true. If the user clicks "no", the return value is false.

  • Prompt (str, value) input box, the return value is the user input content

6. Several Common built-in classes in javascript

1. String

  • Length: length
  • IndexOf (string): Find the position where the substring appears
  • Substr (num1, [num2]): truncates a string.
  • ToLowerCase (): converts to lowercase
  • ToUpperCase (): converts to uppercase
  • Replace (str1, str2): replace str1 with str2

When we assign a value to a variable, this variable is the class Object of the string class.

Var str = 'hello'; // that is, the str variable is a Class Object of the String class. You can call the preceding methods and attributes.

2. date type

  • GetYear () returns the year (two or four digits)
  • GetFullYear () returns the year (4 digits)
  • GetMonth () returns the month from 0 to 11.
  • GetDate () returns date 1-31
  • GetDay () returns the number of weeks 0-6
  • GetHours () return hours 0-23
  • GetMinutes () returns the number of minutes 0-59
  • GetSeconds () returns 0-59 seconds
  • GetMilliseconds () returns timeout seconds 0-99999

3. Mathematics. Math ()

  • Ceil (value): gets the smallest integer greater than or equal to this number, // rounded up
  • Floor (value): returns the largest integer that is less than or equal to this number. // returns an integer down.
  • Round (value): Rounding
  • Min (value 1, value 2): returns a decimal number.
  • Max (value 1, value 2): obtains a large number.
  • Pow (value 1, value 2): obtains the 2nd power of value 1.
  • Random (): random Number (0-------1)
  • Sqrt (value): Square

# P # php tutorial-javascript programming-events and timers # e #

VII. Timer)

How many seconds after the delay to execute some code

  • Format: setTimeout ("function", delay in milliseconds );

[Object Name] = setTimeout ("function", delay in milliseconds );

  • Format: clearTimeout (timer Object Name );

8. Get a preliminary understanding of document in js

  • [Object Name] = document. getElementById ("id name ");

Get an object by id

All attributes of this element can be referenced (an array is returned) in the form of (Object Name );

  • Object Name. innerHTML // get content containing html tags
  • Object Name. innerText // get text content

The innerHTML and innerText attributes indicate setting or retrieving content between a pair of tags.

IX. Events in javascript

  • Specify that the event handler is directly specified in the HTML Tag, which is the most common method of application: <标记 ... ... 事件="事件处理程序">

Events in Javascript:

  • OnLoad: When the page is loaded
  • OnUnload: When the page is closed
  • OnBlur: when the focus is lost
  • OnFocus: Get focus
  • OnClick: When you click
  • OnMouseOver: When the mouse passes
  • OnMouseOut: When the mouse leaves
  • OnMouseDown: When the mouse is pressed
  • OnMouseUp: When the mouse is lifted
  • OnMouseMove: When the mouse moves
  • OnChange: when the content changes
  • OnSelect: when the content is selected
  • Onkeypress: When the keyboard is clicked
  • Onkeydown: When the keyboard is pressed
  • Onkeyup: When the keyboard is lifted
  • OnSubmit: When a form is submitted
  • OnReset: when the form is reset

Job: Draw a div, floating from the upper left corner of the page to the lower right corner

Use javascript to output the 99 multiplication table:

 
Related Article

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.