Home >Web Front-end >JS Tutorial >A complete collection of unknown popular basic JavaScript knowledge (Collection)
Learning JavaScript is very boring and a headache. This article will help you deepen your impression of the basic knowledge. You will use it in your future study. Save it if you need it.
1. JS built-in objects
(1)Number
Creation method:
var myNum=new Number(value); var myNum=Number(value);
Properties and methods:
toString(): Convert to string
using use with using using through through off out out through out out through out out through out out together out right out right out through off out off ’ through ’ through ‐ through ‐ through ‐ through through through through through through through‐‐‐ to toString(): to be converted into a string.
Valueof (): Return to the basic value of a Boolean object (Boolean)
(3) String
var bool = new Boolean(value); var bool = Boolean(value);
lastIndexOf();Reversely returns the index of the character
split(); The string is cut into an array based on special characters
# toUpperCase(); : Put all elements of the array into a string. The element is separated by the specified separator of
POP (): deletes and returns the final element
Push (): Add one or more elements to the end of the array, and return the new length
reverse () ;Reverse array
use using ’ ‐ ’ s ’ ’ s ‐ ‐ ‐ ‐ ‐ ‐ sort(); to
var myDate = new Date(); var myDate = new Date(毫秒值);//代表从1970-1-1到现在的一个毫秒值
属性和方法
getFullYear():年
getMonth():月 0-11
getDate():日 1-31
getDay():星期 0-6
getTime():返回1970年1月1日午夜到指定日期(字符串)的毫秒数
toLocalString();获得本地时间格式的字符串
(6)Math
创建方式:
Math 对象并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math(),像 Math.sin() 这样的函数只是函数,
不是某个对象的方法。您无需创建它,通过把 Math 作为对象使用就可以调用其所有属性和方法。
属性和方法
PI:圆周率
abs():绝对值
ceil():对数进行上舍入
floor():对数进行下舍入
pow(x,y):返回 x 的 y 次幂
random():0-1之间的随机数
round():四舍五入
(7)RegExp
创建方式:
var reg = new RegExp(pattern);
var reg = /^正则规则$/;
规则的写法:
[0-9]
[A-Z]
[a-z]
[A-z]
\d 代表数据
\D 非数字
\w 查找单词字符
\W 查找非单词字符
\s 查找空白字符
\S 查找非空白字符
n+ 出现至少一次
n* 出现0次或多次
n? 出现0次或1次
{5} 出现5
{2,8} 2到8次
方法:
test(str):检索字符串中指定的值。返回 true 或 false
需求:
校验邮箱:
var email = haohao_827@163.com var reg = /^[A-z]+[A-z0-9_-]*\@[A-z0-9]+\.[A-z]+$/; reg.test(email);
二、js的函数
1、js函数定义的方式
(1)普通方式
语法:function 函数名(参数列表){函数体}
示例:
function method(){
alert("xxx");
}
method();
(2)匿名函数
语法:function(参数列表){函数体}
示例:
var method = function(){
alert("yyy");
};
method();
(3)对象函数
语法:new Function(参数1,参数2,...,函数体);
注意:参数名称必须使用字符串形式、最后一个默认是函数体且函数体需要字符串形式
示例:
var fn = new Function("a","b","alert(a+b)");
fn(2,5);
2、函数的参数
(1)形参没有var去修饰
(2)形参和实参个数不一定相等
(3)arguments对象 是个数组 会将传递的实参进行封装
function fn(a,b,c){
//var sum = a+b+c;
//alert(sum);
//arguments是个数组 会将传递的实参进行封装
for(var i=0;i9dbd8b79fcb962734ffe6b5d6cf0701aname8c1ecd4bb896b2264e0711597d40766c
666ac8728054c077fc5cc775c102c5f26ef44c608976f15c1dbf0d10ec38e59754bdf357c58b8a65c66d7c19c8e4d114
5cd6e472395e766622bc5d31b556eb7a
var txt = document.getElementById("txt");
txt.onfocus = function(){
//友好提示
var span = document.getElementById("action");
span.innerHTML = "用户名格式最小8位";
span.style.color = "green";
};
txt.onblur = function(){
//错误提示
var span = document.getElementById("action");
span.innerHTML = "对不起 格式不正确";
span.style.color = "red";
};
2cacc6d41bbb37262a98f745aa00fbf0
onmouseover:鼠标悬浮的事件
onmouseout:鼠标离开的事件
需求:p元素 鼠标移入变为绿色 移出恢复原色
#d1{background-color: red;width:200px;height: 200px;}
aee05862a039576747986e29c62a5b6e94b3e26ee717c64999d7867364b1b4a3
5cd6e472395e766622bc5d31b556eb7a
var p = document.getElementById("d1");
p.onmouseover = function(){
this.style.backgroundColor = "green";
};
p.onmouseout = function(){
this.style.backgroundColor = "red";
};
2cacc6d41bbb37262a98f745aa00fbf0
onload:加载完毕的事件
等到页面加载完毕在执行onload事件所指向的函数
112c8dd940660acc05061dbf2b5db10d54bdf357c58b8a65c66d7c19c8e4d114
5cd6e472395e766622bc5d31b556eb7a
window.onload = function(){
var span = document.getElementById("span");
alert(span);
span.innerHTML = "hello js" (1) will Events and response behaviors are embedded in html tags
Into html and the response behavior is encapsulated with a function
;
’ ’ ’s ’ s ‐ to html ’ s ’ s ‐ ‐ ‐ ‐ ‐ Tags completely separated
cf83b740ad950c39ad357c65fa81dff2
: ****thisKeyword
This is passed through the event function the html tag object
410da9042181efcc0dc0442438e5daa7
2cacc6d41bbb37262a98f745aa00fbf0
3. Default behavior of preventing events
IE:window.event.returnValue = false;
W3c: The passed event object.preventDefault();
//ie:window.event.returnValue = true; .preventDefault();
//IE tag
}else{
alert("ie");
window.event.returnValue = false;
}
// Returning false from the event can also prevent the default behavior of the event
4. Prevent the spread of events
IE: window.event.cancelBubble = true;
W3c: The passed event object.stopPropagation();
if(e&&e.stopPropagation){
# alert("ie");
4. JS bom
(1) window object
Pop-up method:
Prompt box: alert("prompt information");
Confirmation box: confirm("confirmation information" ; :prompt("Prompt information");
There is a return value: If you click Confirm to return the text of the input box, click Cancel to return null
var res = prompt("Please enter your password?");
alert(res );
open method:
window.open("url address");
open("../jsCore/demo10.html");
settimeout (function, millisecond value);
Settimeout (
Function () {
Alert ("xx");
},
3000
); The name of the device);
var timer;
var fn = function(){
alert("x"); );
};
var closer = function(){
clearTimeout(timer);
var closer = function(){
clearTimeout(timer);
setInterval(function, millisecond value) );
clearInterval(timer name)
var timer = setInterval(
function(){
alert("nihao");
},
20 00
);
var closer = function(){
clearInterval(timer);
clearInterval(timer);
">554bdf357c58b8a65c66d7c19c8e4d114 Jump to the home page in seconds. If it does not jump, please5685b03544af1800cce2a6330467ef97Click here5db79b134e9f6b82c0b36e0489ee08ed
type="text/javascript">
var time = 5;
var timer;
timer = setInterval(
function(){
var second = document.getElementById("second");
if(time>=1){
second.innerHTML = time;
time--;
}else{
clearInterval(timer);
location.href="../jsCore/demo10.html";
}
},
1000
);
2cacc6d41bbb37262a98f745aa00fbf0
(2)location
location.href="url地址";
(3)history
back();
forward();
go();
29415e7f3b36c7b62de23ab377874ce2后一页5db79b134e9f6b82c0b36e0489ee08ed
724c8aa3cbc2fd3318a09dcf8d52a4e8
07a1cf4055b0f1271e1dc90428774457
79a790c4cf064000194a88f282915c7b
d2db708b4b1b53ea868930e8b9bc39f6
五、js的dom
1、理解一下文档对象模型
html文件加载到内存之后会形成一颗dom树,根据这些节点对象可以进行脚本代码的动态修改
在dom树当中 一切皆为节点对象
2、dom方法和属性
笔记见代码
相关推荐:
The above is the detailed content of A complete collection of unknown popular basic JavaScript knowledge (Collection). For more information, please follow other related articles on the PHP Chinese website!