JavaScript Introductory QA Summary

Source: Internet
Author: User

Where can the Q1:js be placed?
A1:
1. Put in 2. Put in the external file, the file suffix is. js, with <script src= "Xx.js"/> The external file into HTML;
3. Write in HTML <body> tags, through <script type= "Text/javascript" ></script> to mark this is a section of JS code;

Q2:js comment How to write?
A2:js comments are the same as Java annotations, "//" is a single-line comment, "/* */" is a multiline comment;

Q3: What is a variable?
A3: A variable is a container for storing values;
The method of defining variables in JS: var A; Define a variable A, type VAR (note: All variable types in JS are Var)
Assign a value to a variable: a = 1;
You can also define and assign values to variables at the same time: var a = 1;

How to use the branch judgment statement in Q4:js?
A1: Branch Judgment statement:
if (EXP1)
{
Operation when EXP1 is satisfied
}
Else
{
Operation when EXP1 is not satisfied
}

Give me a chestnut:

1 varA = "man";//define a variable A, and assign him the string "man";2 if(A = = "man")//if the value of a is equal to "man", execute the code inside the curly braces below;3 {4Alert ("Boy! ");//the pop-up window shows "Boy!";5 }6 Else if(A = = "Woman")//if the value of a is equal to "woman", execute the code inside the curly braces below;7 {8Alert ("Girl! ");//the pop-up window shows "Girl!";9 }Ten Else                  //all previous if are not compliant One { AAlert ("Shemale! ");//pop-up window shows "Shemale!" "; -}


Q5: What is a function?
A5: A function is a block of code that accomplishes a particular function;
Explanation: We put the code that completes a particular function into a block of code, give it a name, and use it to invoke it directly by name;
To define a function:
Function name ()
{
function code;
}
Give me a chestnut:

 1  function   add ()  2  { 3  //  This function is to add two numbers, and the popup window displays  4  var  a = 1;  5  var  b = 2;< /span>6  var  sum = a + b;  7   alert (sum);  8 } 


What are the common built-in methods in Q6:js?
A6:
1.document.write ("text displayed on the page"); Write content directly to the HTML output, which is displayed directly on the page
2.alert ("Pop-up window display text"); Warning dialog box (Message dialog box)
3.conform (); Confirmation dialog box (with return values and parameters, see examples)
4.prompt (); Question dialog box (with return values and parameters, see examples)
5.window.open (); Open a new window (multiple optional parameters)
6.window.close (); Close the current window (if you want to close the specified window, first get the specified Window object, obj, and then use this method to close, Obj.close ();)

Give me a chestnut:

1<script type= "Text/javascript" >2     varresult = Confirm ("Are you a boy?") ");//Click the Confirm button to return true,3     if(Result = =true)4     {5Alert ("You're a Boy");6     }7     Else8     {9Alert ("You're a Girl");Ten     } One</script>
1 <script type= "JavaScript" >2     function  tiwen ()3    {  4             var obj = prompt ("What's your name?") "," Xiao Hua ");  // obj is the value entered in the input box, which defaults to the second parameter value: "Xiao Hua" 5             alert (obj);    // warning Bullet box showing results 6     }7 </script>
 1  <script type= "javascript" >2  function   Openwindow () { 3  var  obj = window.open (" http://www.baidu.com "," _blank "," width=600px,height=300px,top=500px,left= 500px " 4  }  5  </script> 
 1  <script type= "javascript" >2  function   Clozen () { 3  var  obj = var  obj = window.open (" http://www.baidu.com "," _blank "," width=600px,height=300px,top=500px,left=500px "   4  obj.close (); //  5  6  window.close (); //  Close the current window  7  }  8  </script> 

Q7: Common ways to get DOM nodes:
A7:
1. var a = document.getElementById ("Value of ID");//Get the node by its ID property
2. var B = Document.getelementsbyname ("Value of name"); Gets the node
3. var c = document.getelelentsbytagname ("tag name") via the label's Name property;//Get node by tag name

hold a chestnut:

 1  function   GetNode () { 2  var  a = document.getElementById (" Add " 3  var  b = Document.getelementsbyname (" Confirm " 4  var  C = Document.getelementsbytagnam E ("Input"  5   alert (a);  6   alert (b);  7   alert (c);  8 } 

       
Q8: How do I manipulate DOM nodes to change the content and style of HTML?
A8:
1. Gets and changes the text within the tag through the innerHTML property of the node:
for chestnuts:

1 function changedom () 2 {3             var divdom = document.getElementById ("Title1"); 4             var text = divdom.innerhtml; 5             alert (text); 6             divdom.innerhtml = "becomes title two"; 7             alert (divdom.innerhtml); 8 }


2. Change the style by using the node's style property:
Give me a chestnut:

1<p id= "Pcon" >hello world!</p>2<script>3    varMyChar = document.getElementById ("Pcon");4Mychar.style.color= "Red";//color set to red5Mychar.style.fontsize= "20";//font size set to;6Mychar.style.backgroundColor = "Blue";//background color set to blue7Mychar.style.display= "None";//Hide Tag Blocks8mychar.style.display= "Block";//Show Tag Block9</script>


3. Increase the style of the node by assigning a value to the class of the node:
Give me a chestnut:

1<style type= "Text/css" >2 . style1{3 color:red;4Font-size:20;5 Backgroundcolor:blue;6     }7</style>8<p id= "Pcon" >hello world!</p>9<script>Ten    varMyChar = document.getElementById ("Pcon"); OneMychar.classname = "Style1"; A</script>



JavaScript Introductory QA Summary

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.