Finishing JavaScript Basics Getting Started learning notes _javascript tips

Source: Internet
Author: User
Tags define function html tags numeric value

Know what a variable is?
A variable is a container for storing information
Declaration of variables
Grammar:

var variable name
variable name = value;
Variable to declare and then assign the value
Variables can be assigned repeatedly
naming rules for variables

    • The variable must begin with a letter;
    • Variables can also start with the $ and _ symbols (although we do not recommend doing so);
    • Variable names are case sensitive (a and a are different variables).

1, statements
The statement ends with a semicolon, and the parser determines the end of the statement if the semicolon is omitted.
Have a good coding habit, to be; End

2. Data type
In JavaScript, a piece of information is a value (value). Values have different types, and the most familiar types are numbers. A string value is one or more words enclosed in quotation marks.
number any numeric value. Numbers can take a small number of points, or can not take 68.57
The character in the string quotation marks. You can use single or double quotes "Hello, World"
Boolean value (Boolean) true or False true
Undefined and Null Undefined This value indicates that the variable does not contain a value. You can empty a variable by setting the value of the variable to null.
Any value associated with an object
Value returned by function function

var A; A is undefined
var a = 6;//a is the number
var a = "Jason";//A is a string

3, What is a function?
A function is a set of JavaScript statements that perform a task
Basic syntax:

function function name () {
 functional code;
}

function name ();
Description
functions define function keywords.
"function name" is the name you take for the function.
"Function code" is replaced with code that completes a particular function.
One of the "second function name" function calls

 function Add2 () {
 var sun = 3 + 2;
 alert (Sun);
 }
 ADD2 ();//Call function Direct write function name directly pop-up functions code
 <input type= "button" value= "click Me" onclick= "add2 ()"/>
 <!--Click the button, call the function , onclick for click event-->

4, output content (document.write)
document.write () outputs content directly from the Web page.
The first type: The output is enclosed with "", and the contents of the "" number are directly output.

document.write ("I love javascript!");

Second: Through variables, output content

var mystr = "Hello World";
document.write (MYSRT)//Direct write variable name, output variable stored content

Third: Output multiple content, the content is connected with the + number.

var mystr = "Hello";
document.write (mystr + "I love Java Script");//multiple content with + sign

Fourth: Output HTML tags, and function, tags use "" enclosed.

var mystr= "Hello";
document.write (mystr+ "<br>");//output Hello, output a newline character
document.write ("JavaScript");

5, Warning (Alert message dialog box)
when we visit the site, sometimes suddenly pop up a small window, which is written with a hint message text. If you do not click "OK", you can not do anything to the Web page, this small window is implemented using alert.
Syntax:alert (string or variable);

var mynum =;
Alert ("hello!");
alert (mynum);

Results: Pop-up message boxes in order (Alert pop-up message dialog box contains a OK button)
Note:
1. No other action can be performed until you click the "OK" button in the dialog box.
2. The message dialog box can usually be used to debug a program.
3. Alert output, can be a string or variable, similar to document.write

6, confirm the Choice (Confirm Message dialog box)
In addition to providing information to users, we also want to get information from users. The Confirm Message dialog box is used here.
The Confirm Message dialog box is typically used to allow the user to make a choice of actions such as: "Are you right?" Wait Pop-up dialog box (includes a OK button and a Cancel button).
Syntax: Confirm (str);
Parameter description: str: The text return value to display in the message dialog box: Boolean
return value:
Returns True when the user clicks on the "OK" button
Returns False when the user clicks the Cancel button
Note: You can tell what button the user clicked by return value

<script type= "Text/javascript" >
 var mymessage=confirm ("Do you like JavaScript?");
 if (mymessage==true) {
   document.write ("Good, Come on!"); 
  } else{
   document.write ("JS function is strong, to learn Oh!");
  }
 </script>

7. Questions (Prompt message dialog box)
Sometimes, not only do you want users to answer yes/no. Instead, you want to get a more specific response. We can use prompt in this situation.
The Prompt pop-up message dialog box is often used to ask for information that requires interaction with the user. The pop-up message dialog box (contains an OK button, a Cancel button, and a text input box).
Grammar:

Prompt (STR1,STR2);
Parameter description:
STR1: Text to be displayed in the message dialog box, not modifiable
STR2: The contents of a text box that can be modified
return value:
1, click the OK button, the contents of the text box will be returned as the function value
2, click Cancel button, will return null

 Function Rec () {
  var score;//score variable, used to store the score value entered by the user.
  score = prompt ("Please enter your score", "the");
  if (score>=90) {
   document.write ("You are great!");
  } else if (score>=75) {
   document.write ("Good Yo!");
  } else if (score>=60) {
   document.write ("to refuel!");
  else{
   document.write ("Work hard!");
  }; 

 <script>
  var myname = prompt ("Enter your Name");
   if (myname!= null && myname!= "") {
    document.write ("welcom to" + myname);
   } else{
    document.write ("Welcom to My Friend");
   }
</script>

8. Open a new window (window.open)
Grammar:

window.open ([URL], [window name], [parameter string])
Parameter Description:
URL: Optional parameter, in the window to display the Web page URL or path. If this argument is omitted, or if its value is an empty string, the window does not display any documents.
Window name: Optional parameter, the name of the window to be opened.
1. The name consists of letters, numbers, and underscore characters.
2. Window Name: Optional, this string is a comma-delimited list of features that declares the name of the window being opened. Can be "_top", "_blank", "_selft", "_parent" and so on.
_blank the target page in a new window
_SELFT Display the target page in the current window
_parent the current entire window position in the frames page to display the target page
_top frames page Displays the destination page in the upper window
3. Windows with the same name can only be created one, and name cannot be the same if you want to create more than one window.
4.name cannot contain spaces.
Parameter string: Optional argument, set window parameters, separated by commas.

Parameter table:
Number of pixels on top of top number window left screen
Left number window left the number of pixels off the left-hand side of the screen
Width number window widths
Height Number window
MenuBar yes,no window has no menu
Toolbar Yes,no window there is no tool bar
ScrollBars Yes,no window There is no scroll bar
Status Yes,no window There is no State bar

 <script type= "Text/javascript" >
  window.open (' http://', ' _blank ', ' width=300,height=200,menubar=no, Toolbar=no, Status=no,scrollbars=yes ')
 </script>

9, close the window (window.close)
Close () Closes the window
Usage:

Window.close ()//close this window
 < Window object >.close ();//close the specified window

For example, close a new window.

 <script type= "Text/javascript" >
  var mywin=window.open (' http://www.imooc.com ');//To store the newly-typed window object in the variable Mywin
  mywin.close ();
 </script>

10, innerHTML Property
The innerHTML property is used to get or replace the contents of an HTML element.
Grammar:

Object.innerhtml
object is an element object that gets, such as fetching an element by document.getElementById ("ID").

11. Change HTML style
Grammar:

Object.style.property=new style;
Note: object is an element object that gets, such as an element obtained by document.getElementById ("id")

12, Show and Hide (Display properties)
Grammar:

Object.style.display = value
Value values:
None this element will not be displayed (and hidden)
Block this element will appear as blocks-level elements (that is, display)

Mychar.style.display = "Block"

13, Control class name (classname property)
classname property to set or return the class attribute of the element.
Grammar:

Object.classname = ClassName
Function:
1. Get the class attribute of the element
2. Specify a CSS style for an element within a Web page to change the appearance of the element

P2.classname = "two";
The above is the basic knowledge of JavaScript learning notes, I hope to help you learn.

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.