JavaScript Learning 4

Source: Internet
Author: User
Tags string methods

HTML 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.

Common HTML Events

The following is a list of some common HTML events:

Events Description
OnChange HTML element Changes
OnClick User clicks on HTML element
onmouseover The user moves the mouse over an HTML element
onmouseout The user moves the mouse away from an HTML element
OnKeyDown The user presses the keyboard key
OnLoad The browser has finished loading the page
What can JavaScript 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.
Special characters

In JavaScript, strings are written in single or double quotation marks.

Because of this, the following instance of JavaScript cannot be resolved:

"We are the so-called" Vikings "from the north."

The string "We are the so-called" is truncated.

How to solve the above problems? You can use a backslash (\) to escape the double quotation marks in the "Vikings" string, as follows:

"We are the so-called \" Vikings\ "from the north."

A backslash is an escape character . The escape character converts a special character to a string character:

The escape character (\) can be used to escape apostrophes, line breaks, quotation marks, and other special characters.

The following table lists special characters that can be escaped with escape characters in a string:

Code Output
\‘ Single quotation marks
\" Double quotes
\\ Back slash
\ n Line break
\ r Enter
\ t tab (TAB)
\b Backspace
\f Page break

There are 5 different types of data in JavaScript:

    • String
    • Number
    • Boolean
    • Object
    • function

3 Types of objects:

    • Object
    • Date
    • Array

2 data types that do not contain any values:

    • Null
    • Undefined

The constructor property returns the constructor for all JavaScript variables.

Method Description
GetDate () Returns the day of the one month (1 ~ 31) from the Date object.
GetDay () Returns the day of the week from a Date object (0 ~ 6).
getFullYear () Returns the year as a four-digit number from a Date object.
GetHours () Returns the hour (0 ~ 23) of the Date object.
Getmilliseconds () Returns the milliseconds (0 ~ 999) of the Date object.
Getminutes () Returns the minute (0 ~ 59) of the Date object.
GetMonth () Returns the month (0 ~ 11) from the Date object.
Getseconds () Returns the number of seconds (0 ~ 59) of the Date object.
GetTime () Returns the number of milliseconds since January 1, 1970.

The global Method number () converts the string to numbers.

strings that contain numbers (such as "3.14") are converted to numbers (such as 3.14).

The empty string is converted to 0.

The other strings are converted to NaN (not a number).

JavascriptRegular Expressions

Regular Expressions (English: Regular expression, often abbreviated in code as regex, RegExp, or re) use a single string to describe and match a series of string search patterns that conform to a certain syntactic rule.

The search mode can be used for text search and text substitution.

What is a regular expression?

A regular expression is a search pattern formed by a sequence of characters.

When you search for data in text, you can use the search pattern to describe what you want to query.

A regular expression can be a simple character, or a more complex pattern.

Regular expressions can be used for all text search and text substitution operations.

Using string methods

In JavaScript, regular expressions are typically used in two string methods: Search () and replace ().

The Search () method retrieves the substring specified in the string, or retrieves a substring that matches the regular expression and returns the starting position of the substring.

The Replace () method is used to replace other characters with some characters in a string, or to replace a substring that matches a regular expression.

JavaScript Learning 4 (RPM)

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.