Javascript Basic Knowledge Collation

Source: Internet
Author: User

The role of 1.Javascript

Javaweb inside

HTML: Responsible for Web page schema, display content, data.

CSS: Responsible for the display of styles and formats.

JavaScript: Responsible for the completion of page interaction, Web page behavior.



Three ways to do 2.Javascript


1). Write directly inside the HTML.

<a href= "Javascript:alert (' Oriental Lightning ')" > Hyperlink </a>

2) The .<script> tag can be written inside the head body or outside the HTML.


3). Using an external file introduction: First create a new JS file demo.js remember: the suffix of all external javascript files. js

<!    DOCTYPE html>



3.javascript common input and output mode


1). Alert ("hello!") pop-up window output.


2). Confirm ("Are you sure you're a man?") ) to return a Boolean value by selecting the Pop-up window.


3). Prompt ("Please enter:"); Enter pop-up window



4.Javascript Data Types JavaScript is a weak language and is used to determine what type

String,number,boolean,array,object,null


1). How to see what type of variable you have, using the TypeOf keyword. For objects such as arrays, typeof only Displays object

If you want to determine if it is an array, use alert (as instanceof array); The result is true for the array type.


2). Undefined undefined type, indicating that you have not initialized or used this variable. The undefined type is false.


3). Special note: In JS except Nan, undefined,0 these three numbers are false, the rest are true.

You can tell if a variable has a value assigned. If the assignment is true no assignment is undefined is false;



5. Conversions of data types


1). Convert numbers to Strings

The number is converted to a string Var a=456;        Alert (typeof a);        A=a.tostring ();//a=456+ ""; Alert (typeof a);


2). Convert strings to Numbers

var b= "one"; alert (b+1); alert (number (b) +1);


3). Note: If you cast a non-numeric value to number, you will get a Nan value;

Using parseint, you can convert several numbers that begin with a string to int, and if the start is not a number, you will only get Nan.

such as Var b= "12px";

Alert (parseint (b)); You can get 12 to change the font size and so on.



Objects of 6.Javascript

Create a Person object function person (name,age) {this.name=name;this.age=age;this.address= "Chengdu, Sichuan";//If you do not use this declaration, This variable is just a local variable, not a property of a class. var m=10;//creation Method This.say=function () {alert (this.name+ "," +this.age);}} Instantiate an object var p1=new person ("Zhang Fei", "P1.name"), Alert (p1.name+ "," +p1.address "alert (p1[" name "]+", "+p1[" address "]);   You can also use alert (typeof p1); Get Objectalert (P1 instanceof person); Get True//In the case of JS, for the object, you can get the properties of the object through for-in, which can be used to see what properties are in the frame. For (Var A in P1) {alert (a);//alert (A + ":" +p1[a]);} Another way to define a function: var x=function () {alert ("X")}//at this point x is a function



7. Date Objects

var d=new date ();d Ocument.write (d.getfullyear () + "year" + (D.getmonth () +1) + "month" +d.getdate () + "Day" + "Week" +d.getday ())



8. String

var str1=new String ("abc"), var str2= "abc"; alert (STR1==STR2); The result is true without Equalsvar s=str2.concat ("Hello", "World");



9. Arrays

1).

var as=new Array (); As.push (one); As.push (12);

2).

var mycars=new Array (); mycars[0]= "Saab"; mycars[1]= "Volvo"; mycars[2]= "BMW";

3).

var as=new Array (11,12,13,14,15,222, "15");

4).

var as=["11", 12,1,2];



10.with function

With (document) {//This time all the code in curly braces is based on document as the root object, when using Write (XXX) is equal to document.write (XXX); write (); write (); write (); Write ();//Use alert is also allowed for alert ("ABC");}



11. Event Handling

<! Doctype html>    










Javascript Basic Knowledge Collation

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.