wangchloe / chloe-test Goto Github PK
View Code? Open in Web Editor NEW:hatching_chick:Chloe's personal test
:hatching_chick:Chloe's personal test
以下内容若有问题烦请即时告知我予以修改,以免误导更多人。
这里的数据类型是按照typeof返回来分类,也不是很懂有些教程写数据类型还有null,求解答数据类型应该是哪几类?
number 数字
string 字符串
boolean 布尔
undefined 未定义
function 函数
null 空对象
NaN和任何数据类型都不相等,包括自己
isNaN() 非数字->true 数字->false
if(num == parseInt(num))
子函数可以使用父函数的全局变量
变量的遮蔽
全局变量和局部变量同名
就近原则->在函数里优先使用自己的变量
== -> != === -> !==
前缀 | 全称 | 含义 | 示例 |
---|---|---|---|
o | object | 一个对象,一个元素 | oDiv |
a | array | 一组元素 | aLi |
s | string | 字符串 | sUserName |
i | integer | 整数 | iCount |
f | float | 浮点数 | fPrice |
b | boolean | 布尔 | bOk |
fn | function | 函数 | fnSucc |
re | RegExp | 正则 | reMailCheck |
number + ''
eg:12 + '' -> '12'
任何标签都可以添加事件,任何属性都可以修改
以下内容若有问题烦请即时告知我予以修改,以免误导更多人。
<script>
function setValue() {
//省略获取元素oBtn, oBtn2的伪代码
oBtn.value = 'bbb';
oBtn2['value'] = 'bbb';
}
</script>
凡是**.** 出现的地方都可以用中括号替代
非首单词的首字母大写并去掉-符
<style>
.complex {
margin-left: 10px;
}
</style>
<script>
function setStyle() {
var oC = document.getElementsByClassName('complex')[0];
oC.style.marginLeft = '10px';
}
</script>
- margin-top: -变化的高度/2
- margin-left: -变化的宽度/2
<a href="#">点击会刷新页面</a>
<a href="javascript:;">点击不会刷新页面!(推荐使用)</a>
<!-- javascript:;相当于一个伪协议 -->
<script>
function clickEg() {
//获取一组按钮 ps:js一组元素不能一起改变样式或设置事件
var aBtn = document.getElementsByTagName('button');
for(var i = 0; i < 3; i++) {
aBtn[i].onclick = function() {
//循环中的i变量此时已自增至3
//aBtn[i].style.background = '#f00';
//当前事件发生的对象 aBtn[i]为this
this.style.background = 'f00';
}
}
}
</script>
1.加载整个页面的标签和属性
2.过滤不符合W3C标准的标签和属性(高级浏览器)
3.执行js -> window.onload
1.document.getElementById('id');
2.document/obj.getElementsByTagName('TagName');
3.document/obj.getElementsByClassName('ClassName');
兼容:Chrome、FF、IE9+
obj.getElementsByClassName
<script>
function getByClass(obj, sClass) { // obj为从哪个父级下面查找类为sClass的元素
if(obj.getElementsByClassName) { // IE8- -> undefined 高级浏览器 -> function
return obj.getElementsByClassName(sClass); // 高级浏览器
} else { // IE8
var aEle = document.getElementsByTagName('*');
var arr = [];
for(var i=0; i<aEle.length; i++){
var temp = aEle[i].className.split(' ');
if(findInArr(sClass, temp)) {
arr.push(aEle[i]);
}
}
return arr;
}
}
</script>
- getElementById只能从document下获取
var oDiv = document.getElementById('id');- getElementsByTagName/getElementsByClassName可以从document下获取,也可以从父级下获取
var oDiv2 = document.getElementsByClassName('ClassName')[0];
var oDiv3 = oBox.getElementsByTagName('TagName')[0];
<script>
function getStyle(obj, name){ //元素,样式名称
if(obj.currentStyle) { // Chrome、FF -> undefined IE -> object
// IE系
return obj.currentStyle[name]; // 兼容IE系
} else {
// Chrome、FF
return getComputedStyle(obj, false)[name]; // 兼容高级浏览器(Chrome、FF、IE9+)
}
}
</script>
简化
<script>
function getStyle(obj, name){ //元素,样式名称
return (obj.currentStyle || getComputedStyle(obj, false))[name];
}
// 调用
console.log(parseInt(getStyle(oDiv, 'heihgt')));
</script>
<script>
function rnd(n, m) {
return parseInt(Math.random() * (m - n) + n);
}
</script>
应用:随机变色
<script>
// rgb色值范围[0, 255]
oDiv.style.background = 'rgb(' + rnd(0, 256) + ',' + rnd(0, 256) + ',' + rnd(0, 256) + ')';
</script>
以下内容若有问题烦请即时告知我予以修改,以免误导更多人。
参数中的数组,函数中可以不需要定义参数
<script>
sum(12, 5, 6);
function sum() {
console.log(arguments[1]); // 5
}
</script>
style.xxx
oDiv.style.width = '300px';
className
oDiv.className = 'active';
cssText
批量设置样式
oDiv.style.cssText = 'width: 300px; height: 300px';
str[i]的兼容问题
获取字符串中的第i+1个
str[i] 兼容:高级浏览器及IE8+
IE7 -> undefined
str.charAt(i) 全兼容
str.indexOf('w'); 查找w在字符串中的位置 返回值:成功 -> w在字符串中的位置 失败 -> -1
从右往左倒序查找,返回的索引值与indexOf()规则相同
str.search('w'); 与indexOf()规则相同 常用于正则
str.substring(开始位置, 结束位置); 截取字符串,包含开始位置,不包含结束位置
str.substring(开始位置); 截取字符串 从开始位置一直截取到最后
str.substr(开始位置, 截取字符串长度); 定长截取字符串
str.slice(开始位置, 结束位置); 截取字符串
str.match('w'); 在字符串中匹配w 常用于正则 返回值:成功 -> 匹配的w 失败 -> null
str.split('w'); 切割字符串 返回值类型:数组
str.toUpperCase(); str转大写
str.toLowerCase(); str转小写
str.replace('xxx', 'yyy'); 常用于正则 参数:被替换内容,替换内容
<script>
var str = 'xxa';
str.replace('x','y');
str2 = str.replace('a', 'b');
str3 = str.replace('x', 'y').replace('a', 'b');
alert(str); // xxa
alert(str.replace('x', 'y')); // yxa
alert(str2); //xxb
alert(str3); //yxb
</script>
<script>
var str = 'xxxy';
var str2 = str.replace('xxx', function(s){
alert(s); // xxx 被替换字符 数据类型:string
var str2 = '';
for(var i = 0; i < s.length; i++) {
str2 += '*';
}
return str2; // 替换后的内容
});
alert(str2); // ***y
</script>
a-> 0x61 -> 97
b-> 0x62 -> 98
z -> 0x7A -> 122
###判断浏览器的类型
window.navigator.userAgent
eg:
<script>
if(window.navigator.userAgent.indexOf('Chrome') != -1) {
console.log('Chrome');
} else if(window.navigator.userAgent.indexOf('Firefox') != -1) {
console.log('Firefox');
} else if(window.navigator.userAgent.indexOf('MSIE7.0') != -1) {
consolle.log('IE7');
} else {
console.log('others');
}
</script>
###判断上传文件格式
eg:
<script>
var index = str.lastIndexOf('.');
var type = str.substring(index+1); //返回文件类型名
</script>
Array()只传一个参数时表示定义一个新数组的长度
new Array(10); 定义一个长度为10的数组
高级排序 数值排序
- 从小到大
arr.sort(function(n1, n2){
return n1-n2;
});
- 从大到小
arr.sort(function(n1, n2){
return n2-n1;
});
<script>
var arr1=[1,2,3,4];
arr1.splice(1, 0, 'a', 'b'); //添加:在1后添加'a','b' 返回值:返回空数组
var arr2=[1,2,3,4];
arr2.splice(1, 2); //删除:删除2、3 返回值:返回删除的各项
var arr3=[1,2,3,4];
arr3.splice(1, 1, 8, 88, 888) //修改:先删除再添加 把2改为8,88,888 返回值:返回删除的各项
</script>
splice模拟方法
1) arr.push(c); -> arr.splice(arr.length, 0, c);
2)arr.unshift(c); -> arr.splice(0, 0, c);
3)arr.pop(); -> arr.splice(arr.length-1, 1);
4)arr.shift(); -> arr.splice(0, 1);
json格式:{name:value,name2:value2, ...}
json标准格式:{"name":value, "name2":value2, ...}
所有键名需双引号,键值非数字时需加引号
键值对没有json.length
json的name是唯一的
获取json值: json.name 或者 json['name']
添加/修改: json.aaa = 'bbb'; 或者 json['aaa'] = 'bbb';
删除: delete json.c; 或者 delete json['c'];
判断json内某个属性是否存在
<script>
var json = {a: 1, b: 2};
alert('c' in json); // false 属性c不存在
</script>
Math.random()
0-1随机数(不包含1)Math.abs(num)
绝对值Math.max(num1, num2, ...)
最大数Math.min(num1, num2, ...)
最小数Math.floor(num)
向下取整 12.4 -> 12 12.6 -> 12Math.ceil(num)
向上取整 12.5 -> 13 12.1 -> 13Math.pow(n, m)
n的m次方 Math.pow(2, 3)=8;Math.sqrt(num)
num开平方 Math.sqrt(9)=3;Math.round(num)
四舍五入 12.1 -> 12 12.6 -> 13
num.toFixed(保留小数个数);
保留几位小数(自动四舍五入)
<script>
try {
// code
} catch(ex) { // exception
console.log(ex.message); // 查看错误信息
// 错误的提示信息
// 补救的代码
}
</script>
以下内容若有问题烦请即时告知我予以修改,以免误导更多人。
eg: var a; // undefined
function show(a) {}
show(); // undefined
eg: oDiv.aaa; // undefined
虽然这个不建议使用,但还是聊聊这个东西是怎么用的吧
eval能把字符串里面的代码转换成js能理解的程序,把引号中的拿出来运行
<script>
var a = '[1, 2, 3]'; //字符串
alert(eval(a)); // 1, 2, 3 '[1, 2, 3]' -> [1, 2, 3]
</script>
<script>
function toTen(num) {
if(num < 10) {
return '0' + num;
} else {
return '' + num; // 函数的返回类型最好保持一致
}
}
</script>
简化
<script>
function toTen(num) {
return num < 10 ? '0' + num : '' + num;
}
</script>
时间单位是毫秒
时间单位是毫秒
<script>
// 定时器先关后开
var bSin = false;
var timer = setInterval(function() {
if(bSin) {
return;
}
bSin = true;
}, 30);
function clear() {
clearInterval(timer);
bSin = false;
}
</script>
<script>
// 获取时间
var oDate = new Date();
oDate.getFullYear(); // 获取年
oDate.getMonth(); // 获取月,从0开始,获取+1,设置-1 !important
oDate.getDate(); // 获取日
oDate.getDay(); // 获取星期,星期天 -> 0,星期一 ~ 星期六 -> 1 ~ 6
oDate.getHours(); // 获取小时,记得加s,下同 !important
oDate.getMinutes(); // 获取分钟
oDate.getSeconds(); // 获取秒
oDate.getMillseconds(); // 获取毫秒
oDate.getTime(); // 时间戳 当前时间距离1970/1/1凌晨的毫秒数
</script>
<script>
// 设置时间
var oDate = new Date();
oDate.setFulllYear(2017, 11, 13); // 设置年、月、日 月份设置时-1
oDate.setHours(0, 0, 0, 0); // 设置时、分、秒、毫秒
// 获得时间戳
oDate.getTime(); // 设置后的时间距离1970/1/1凌晨的毫秒数
</script>
oDate.setDate(31); // 假设本月有30天会跑到下个月的第一天 会自动进位
oDate.setDate(0); // 会跑到上个月的最后一天
<script>
var oDate = new Date();
oDate.setMonth(oDate.getMonth() + 1); // 当前月份+1
oDate.setDate(0); // setDate(0);
alert(oDate.getDate());
</script>
<script>
var oDate = new Date();
oDate.setDate(1); // setDate(1);
alert(oDate.getDay());
</script>
<script>
var oDate = new Date();
oDate.setMonth(oDate.getMonth()+1); // 当前月份+1
oDate.setDate(0); // setDate(0);
alert(oDate.getDay());
</script>
eg: oDiv1.onmouseout = oDiv2.onmouseout = function() {};
this: 当前方法属于谁,this就是谁
this默认属于window
定时器里的this不能直接使用,原因:this指向了window
1.定时器中的this不指向元素,指向window
解决:在定时器外保存this
oBtn.onclick = function() {
var _this = this;
setTimeout(function(){
_this.style.background = '#f00';
},1000);
}
2.调用封装函数使用this,this不指向元素,指向window
3.(低级浏览器attachEvent)事件绑定里面的this 报错
用处:
<script>
function clickEg() {
//获取一组按钮 ps:js一组元素不能一起改变样式或设置事件
var aBtn = document.getElementsByTagName('button');
for(var i = 0; i < 3; i++) {
aBtn[i].onclick = function() {
//循环中的i变量此时已自增至3
//aBtn[i].style.background = '#f00';
//当前事件发生的对象 aBtn[i]为this
this.style.background = 'f00';
}
}
}
</script>
闭包写法:
<script>
function clickEg() {
//获取一组按钮 ps:js一组元素不能一起改变样式或设置事件
var aBtn = document.getElementsByTagName('button');
for(var i = 0; i < 3; i++) {
(function(index) {
aBtn[i].onclick = function() {
aBtn[index].style.background = 'f00';
}
})(i);
}
}
</script>
<script>
for(var i=0; i<2; i++) {
setTimeout(function(){
alert(i);
}, 2000);
} // 结果:两秒后alert两次2,两秒后i已为2,然后执行两次循环
</script>
闭包写法:
<script>
for(var i=0; i<2; i++) {
(function(a){
setTimeout(function(){
alert(a);
},2000);
})(i);
} // 结果:两秒后alert 0、1
</script>
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.