Math
Basic properties applied to Math objects (common)
abs,ceil,floor,random,min,max,round
Math belongs to a mathematical object
console.log("abs:"+Math.abs(-19)); // Absolute console.log("ceil:"+ math.ceil (-3.1)); // console.log("floor:"+ math.floor (-3.1)); // Fetch the whole console.log("round:"+ math.round (3.4)); / / round the console. The log (" Max: "+ Math. Max (100000, 2, 3, 4, 5)); / / for maximum console. The log (" min: "+ Math. Min (100000, 2, 3, 4, 5)); Console. log("random:"+ math.random ()); Returns a random number between 0 and 1Copy the code
String
String object properties:
Constructor reference to the function that created the object
Length Indicates the length of the string
Prototype lets you add properties and methods to objects
Js is a weakly typed variable language, variables are objects [PHP/JSP /asp….]
var str = "dscsdvadg";
console.log(typeof(str)); //string
console.log(str.length);
for (var i = 0; i < str.length; i++) {
document.write(str[i] + "<br />");
}
var str = new String("dscsdvadg");
console.log(typeof(str)); //object
console.log(str.length);
for (var i = 0; i < str.length; i++) {
document.write(str[i] + "<br />");
}
Copy the code
Method of a String object
var str = "javascript is a very important language";
Copy the code
ChanAt (n) function — returns the character at the specified position (n)
document.write("charAt(n):" + str.charAt(9));
Copy the code
The charCodeAt(n) function returns the Unicode encoding of the character at position (n)
document.write("charCodeAt(n):" + str.charCodeAt(9));
Copy the code
Indexof () – Returns the position of subString in string and returns -1 counting from 0 if not found
document.write("indexOf(n):" + str.indexOf("very"));
Copy the code
var email = "asxdascfasvfsf@sdvs";
document.write("indexOf(n):" + email.indexOf("@"));
document.write("lastIndexOf(n):" + email.lastIndexOf("@"));
Copy the code
Case interchange
lowercase
document.write(str.toLowerCase());
Copy the code
A capital
document.write(str.toUpperCase());
Copy the code
Concat merges multiple strings
Document.write (str.concat(" csdCasdvdvaedb ", "merge multiple strings "));Copy the code
The replace method is used to replace some characters in a string with other characters, or to replace a substring that matches a regular expression.
Replace (old,new) function — replaces oldStr1 in the string with newstr2
Var str2 = "get up where you fell "; Document. The write (" the replace () : "+ str2. Replace (" up", "sleep"));Copy the code
The interception
Substr, substring, slice
Var study = "Good good, day day up "; //substr(start,length) //(2,4) count from 2 to 4 to learn, day //substring(start,end) count from start to end //(2,4) count from 2 to end (learn) count from 0 to start, excluding end document.write(study.substr(2, 4) + "<br />"); Document. write(study. Substring (2, 4) + "<br />"); / / learning document. Write (study. Slice (1, 3) + "< br / >"); // Start from 1 to 3, excluding 3Copy the code
The Date object
Commonly used method
var d = new Date(); / / year - month - day - - - seconds - week document. Write (" in return: "+ d.g etFullYear () +" "); Document.write (" return year :"+ d.goetMonth ()()+1)+" month "); //+1)+ document.write(" return date :"+d.getDate()); //1~31 document.write(" return week :"+ d.gettday ()); //0~6 document.write(" return hour :"+ d.gethours ()+" dot "); / / 0-23 document. Write (" return minutes: "+ d.g etMinutes () +" "); // document.write(" return seconds :"+ d.goetseconds ()+" seconds "); / / 0-59Copy the code
Application of object (output system time)
(a)
var dt=new Date(); Var STR =" "; STR + = dt. GetFullYear () + "years"; STR + = (dt, getMonth () + 1) + "month"; STR + = dt. GetDate () + ", "; str+=(dt.getHours())+":"; str+=(dt.getMinutes())+":"; STR + = (dt) getSeconds ()) + "week". Switch (dt.getDay()){case 0: STR +=" break "; Elseif STR +=" break "; Elseif STR +=" break "; Elseif STR +=" break "; Elseif STR +=" break "; Elseif STR +=" break "; Elseif STR +=" break "; } document.write(str);Copy the code
(2)
var dt =new Date(); With (dt){var STR =" "; STR + = getFullYear () + "years"; STR + = (getMonth () + 1) + "month"; STR + = getDate () + ", "; STR + = getHours () + ":"; STR + = getMinutes () + ":"; STR + = (getSeconds ()) + "week". Switch (dt.getDay()){case 0: STR +=" "; break; Elseif STR +=" 1 "; break; Elseif STR +=" 2 "; break; Elseif STR +=" 3 "; break; Elseif STR +=" 4 "; break; Elseif STR +=" 5 "; break; Elseif STR +=" 6 "; break; } } document.write(str);Copy the code
The countdown
Structure:
< div class = "box" > < h3 > birthday countdown < / h3 > < p > distance and birthday < b id = "d" > < / b > day < b id = "t" > < / b > < b id = "m" > < / b > points < b id = "s" > < / b > s < / p > < / div >Copy the code
Style:
window.onload=function(){ function demo(){ var d=document.getElementById("d"); var t=document.getElementById("t"); var m=document.getElementById("m"); var s=document.getElementById("s"); Var time = "2021.6.27 00:00"; var now=new Date(); var distance=parseInt((Date.parse(time) - Date.parse(now))/1000); s.innerHTML=parseInt(distance%60); console.log(s); m.innerHTML=parseInt((distance/60)%60); t.innerHTML=parseInt((distance/3600)%24); d.innerHTML=parseInt(distance/3600/24); setTimeout(demo,1000) } demo(); }Copy the code