JavaScript
定时器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var timer = setInterval(func,3000)
function func () {
alert('func')
clearInterval(timer)
}
</script>
</head>
<body>
</body>
</html>
数组
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var array = [1,2,3,'hude',true]
alert(array.length)
alert(array[2])
var newlist = array.join('-')
alert(newlist)
alert(typeof(newlist))
var alist=[2,1,4,5,3]
alist.reverse
alert(alist)
var alist=[2,1,4,5,3,4,3,7,4]
alert(alist.indexOf(4))
var alist06 = ['别人手牵手','我牵我的狗','走一走','游一游','看谁不爽咬两口']
alist06.splice(2,2,'啧啧','啧啧')
alert(alist06)
</script>
</head>
<body>
</body>
</html>
循环
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
window.onload = function (){
var aList = ['鹅厂','狼厂','猪场','粮厂','社会厂']
var oDiv=document.getElementById('box')
str=''
for (var i=0;i<aList.length;i++){
var value = aList[i]
str+='<li>'+value+'</li>'
oDiv.innerHTML=str
}
}
</script>
</head>
<body>
<div id="box">
</div>
</body>
</html>
去重
未注释的方法是重点
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var alist =[2,3,5,1,4,2,4,2,4,3,3,6,4,7,4,8,5,5,4,4,7,3,5,4,6,4]
var list=[]
for (var i=0;i<alist.length;i++){
var value=alist[i]
if (alist.indexOf(value)==i){
list.push(value)
}
}
alert(list)
</script>
</head>
<body>
</body>
</html>
简易动画
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#box {
width: 200px;
height: 200px;
background-color: aqua;
position: absolute;
left: 0px;
}
</style>
<script>
window.onload = function(){
var oDiv = document.getElementById('box')
setInterval(func,30)
var distance=0
var num=3
function func(){
console.log('a')
distance+=num
if (distance>300){
num=-3
}else if (distance==0){
num=3
}
console.log(distance)
oDiv.style.left=distance+'px'
}
}
</script>
</head>
<body>
<div id="box"></div>
</body>
</html>