Hare's JS learning notes - merging and updating
The words written in the front
Classroom Notes 1
1. Basic content:
<script type = “text/javascript”>
//jscode
<script>
<script src=”myjs.js”> <script>
//There is no need to write<script>tags in the js file. Write code directly.
Execute in sequence.
Single row//
Multiple lines/**/
var a = 1;
//Use var to define, and the type will be automatically declared as a data type.
if(boolean){
//jscode
}
else{
//jscode
}
function myFun1(){
/jscode
}
//Use the function function name () {} to define the function. Call by writing the function name () in the corresponding place.
2. Common simple methods
Document. write (string/variable, etc., use+connection)
//It is used to directly write content to the html output stream.
Alert ([str]) warning message dialog
//Warning: A single button dialog box pops up. At this time, do not execute the following content, and wait for the user to click the button. monopoly.
Confirm ([str]) confirmation message dialog
//Str is the display content. The return value of this method is boolean. Click OK to return true.
Prompt ([str1], [str2]) question message dialog
//Str1 is the title content, which cannot be modified by the user. str2 is the content displayed by default in the edit box, which can be modified by the user. After the user confirms, this value is returned.
Window. open ([url], [name], [specs], [replace]) method opens a new window
//Name specifies the target attribute or the name of the window, and specs specifies some parameters, such as full screen, width, and height.
Window. close() method
//Close the page, for example, windows. close() closes the current page, and mypage. close() closes the mypage page.
Classroom Notes 2
1. Give me control!
When parsing html documents with DOM, first build a complete parse tree in memory, that is, all elements in html are parsed into hierarchical nodes on the tree, and then you can perform operations on these nodes.
Get element by id
document.getElementById(id);
//For example, var element1=document. getElementById ("id1");
Get element group through tagname
document.getElementsByTagName(tagname);
//If it is *, match all elements.
//String is case insensitive
Get element group through name
document.getElementsByName(name);
Although you can use document. getElementsByTagName (tagname) [3]; Get the fourth tagname element, but it is recommended to use id
With the above method of obtaining elements (groups), the following can naturally control the style.
Text between element tags: obj.innerHTML;
Background color: obj. style. backgroundColor;
//Note that the element does not have a background color attribute, only a style attribute, and the background color attribute is a style attribute.
Color: obj.style.color;
Width: obj.width;
Set whether to display the element by setting the value of the display attribute of style
For example, obj.style.display="none"// Do not display
obj.style.display = “block”; // Block display
The className attribute of the element controls or returns the class attribute of the element.
For example: obj.className="div2";
Classroom Notes 3
1. Operator
var a=5;b=6;
var result = a>b;
var result2 = ! (a>b);
//The result of result is false of boolean type
//The result of result2 is true of boolean type
Arithmetic operator>comparison operator>logical operator (| |,&&, etc.)>"=" assignment symbol
2. Array
var myArr = new Array();
var myArr = new Array(66,80,90,77,59);
var myArr2 = [1,2,3];// Why does sbjs use [] qvq
var a = myArr[0]; // a = undefined
myArr2[3] = 5;
var b =myArr2.length; // ->a = 4
var myArr = [[1,2,3],[4,5,6]];
var myArr1 = new Array();
myArr1[0] = Array();
myArr1[0][0] = 1;
References