javascript notes

JavaScript 
is an independent language, and the browser has a js interpreter. It
exists in HTML. The
javascript code exists in the form
-Head
<script>
//javascript code
alert(123)
</script>

<script type='text/javascript'>
/ /javascript code
alert(123)
</script> -file
<
script src='js file path'> </script>
PS: JS code block is placed at the bottom of the <body> tag
Comments
Single-line comments//
Multi-line comments/ * */
variable:
python:
name = 'jia'
javascript:
name = 'jia'#global variable
var name = 'eric' #Local variable
Write JS code: - Write in html file - temporary, basic data type of browser's terminal console Number age = 18; string a = 'ji aia ' a.charAt (index position) a .substring(start position, end position) get the subsequence according to the index a.length get the current string length a.trim() remove blanks a.trimLeft() a.trimRight() a.concat(b, ... ) splicing a.indexOf(subsequence, start) subsequence position a.lastindexOf(subsequence, start) subsequence position (from the back) a.slice(start,end) slice (for changes to the original data, the subsequence is a new string) a.toLowerCase() uppercase a.toUpperCase() lowercase




















a.split(delimiter,limit) split (split the string according to delimiter, return limit split sequences)
list (array)
a = [1,2,3,4,5,7]
obj.length The size of the array
obj .push(ele) appends an element at the end
obj.pop() gets an element at the end
obj.unshift(ele) inserts an element at the head
obj.shift() inserts an element at the head
obj.splice(start, deleteCount, value,...) Insert, delete, replace array elements
start: index number
deleteCount: number of deletions (1/0)
value,...: elements to insert or
replace At the position of index n, delete 0 elements and put them into val)
obj.splice(n,1,val) replaces the element at the specified position (at the position of index n, delete an element and put it into val)
obj.splice(n, 1) deletes an element at the specified position (at index n, deletes 1 element)
obj.slice() slices
obj.reverse() reverses
obj.join(sep) joins array elements to Build a string
obj.concat(val,...) concatenate array
obj.sort() sort array elements
dictionary
s = {'k1':'v1','k2':'v2'}
boolean
lowercase true false

for loop
1, the loop is his index
a = [11,22,33,44]
for (var item in a){
console.log a(item);
}

s = {'k1':'v1 ','k2':'v2'}
for (var item in s){
console.log a(item);
}
2,
a = [11,22,33,44]
for (var i=0;i<a.length;i++){

}

conditional statement
if(condition){

}else if(condition){

}else{
}

!=
== Value equal
=== Value and type are equal
&& and
|| or

function:

function function name(a,b,c){

}
function name(1,2,3)


timer:
setInterval('executed code',interval time); create timer
function func(){
//Get the content of the specified tag according to the ID, define a local variable to receive
var tag = document.getElementById('id');
//Get the content inside the tag
var content = tag.innerText ;

var f = content.charAt(0);
var l = content.substring(1,content.lengh);
//String concatenation
var new_content = l + f;
tag.innerText = new_content;
}
setInterval('func() ',1000);


Dom
1, find the tag
Get a single element document.getElementById('i1')
Get multiple elements (list) document.getElementsByTagName('div')
Get multiple elements (list) document.getElementsByClassName('c1' )
Get multiple elements (lists) document.getElementsByName('div')
a, directly find
document.getElementById('i1') to get a tag according to the ID
document.getElementsByTagName('div') and get the tag set
document according to the tag name.getElementsByClassName('c1') gets the tag collection according to the class attribute
document.getElementsByName('div') gets the tag set
b according to the name attribute, indirect
tag = document.getElementById('i1')
parentNode // parent node
childNodes // all child nodes
firstChild // first child node
lastChild // last a child node
nextSibling // next sibling node
previousSibling // previous sibling node

parentElement // parent node label element
children // all child labels
firstElementChild // first child label element
lastElementChild // last child label element
nextElementtSibling // next sibling tag element
previousElementSibling // previous sibling tag element

2. Operate tag
a, innerText
to get the text content in the
tag. Tag.innerText reassigns

the text inside the
tag. Tag.innerText = ''
b,className
tag.className directly do the whole operation
tag.classList.add('style name') Add the specified style
tag.classList.remove('style name') Delete the specified style

PS:
<div onclick='func()';'>Click me</div>
<script>
function func(){

}
</script>
c, checkbox
gets the value of the
checkbox object.checked
sets the value of the
checkbox object.checked = true

event:
<div onclick='Function(123)'></div>

<script>
...
<script>
Others:
alert() pop-up
console.log() output content

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325264932&siteId=291194637