JavaScript初级
- 数组
迭代方法---- forEach
可以对数组进行遍历,forEach 没有返回值。
第一个参数是元素(el)
第二个参数是下标(index)
第三个参数是数值本身(arr)
<script type="text/javascript">
var a =[`b`,`c`,`v`,`k`]
a.forEach(function(el,index,arr){
console.log(`元素:`+el,`下标:`+index)
console.log(arr)
})
</script>
some和every
some:数值里面有一个满足条件,结果就是true
every:数值里面有一个不满足条件,结果就是false
some
<script>
var a = [
{name :`张张`,age:20},
{name :`郭郭`,age:23},
{name :`孙孙`,age:21},
{name :`莉莉`,age:26},
]
var more23 = a.some(function(el,index,arr){
return el.age > 23
})
console.log(more23)
</script>
every
<script>
var a = [
{name :`张张`,age:20},
{name :`郭郭`,age:23},
{name :`孙孙`,age:21},
{name :`莉莉`,age:26},
]
var more23 = a.some(function(el,index,arr){
return el.age > 23
})
console.log(more23)
var more23 = a.every(function(el,index,arr)
{
return el.age > 23
})
console.log(more23)
</script>
filter
<script>
var a = [
{name :`张张`,age:20},
{name :`郭郭`,age:23},
{name :`孙孙`,age:21},
{name :`莉莉`,age:26},
]
var newA = a.filter(function(el,index,arr){
return el.age >23
})
console.log(newA)
</script>
map
可以对数组做批量处理
<script>
var a = [
{name :`张张`,age:20},
{name :`郭郭`,age:23},
{name :`孙孙`,age:21},
{name :`莉莉`,age:26},
]
var names = a.map(function(el,index,a){
return el.name
})
console.log(names)
</script>
<script>
var a = [
{name :`张张`,age:20},
{name :`郭郭`,age:23},
{name :`孙孙`,age:21},
{name :`莉莉`,age:26},
]
var names = a.map(function(el,index,a){
return el.name
})
console.log(names)
var newA = a.map(function(el,index){
el.age = el.age+1
return el
})
console.log(newA)
</script>
<script>
var a = [
{name :`张张`,age:20},
{name :`郭郭`,age:23},
{name :`孙孙`,age:21},
{name :`莉莉`,age:26},
]
a.reduce(function(pre,el,index,a){
console.log(pre,el,index)
return pre+1
},0)
</script>
reduce求和
<script>
var a = [
{name :`张张`,age:20},
{name :`郭郭`,age:23},
{name :`孙孙`,age:21},
{name :`莉莉`,age:26},
]
var ageSum = a.reduce(function(pre,el,index,a){
return pre+el.age
},0)
console.log(ageSum)
</script>
2.DOM事件
什么事件?
JavaScript 可以触发这些事件,例如:点击,单击,键盘按下抬起等这些称为事件。
鼠标事件:
单击事件(onclick)
添加的事件方式一:
<body>
<div id="bbc" onclick="a()">
按钮
</div>
</body>
<script>
function a(){
console.log(`点击了`)
}
</script>
添加事件方式二
<body>
<div id="bbc" >
按钮
</div>
</body>
<script>
var bbc = document.getElementById(`bbc`)
bbc.onclick = function(){
alert(`点击了`)
}
</script>
click是事件的类型,function是事件的处理函数
通过监听的方式添加事件
<body>
<div id="bbc" >
按钮
</div>
</body>
<script>
var bbc = document.getElementById(`bbc`)
bbc.addEventListener(`click`, function(){
alert(`点击了`)
})
</script>
鼠标双击
<body>
<div id="bbc" >
按钮
</div>
</body>
<script>
var bbc = document.getElementById(`bbc`)
bbc.ondblclick = function(){
alert(`双击了`)
console.log(`双击了`)
}
</script>
鼠标按下(onmousedown)和抬起(onmouseup)
鼠标在anniu(元素)中,按下和抬起操作,会触发事件
<body>
<div id="bbc" >
anniu
</div>
</body>
<script>
var bbc = document.getElementById(`bbc`)
bbc.onmousedown = function(){
console.log(`鼠标按下`)
}
bbc.onmouseup= function(){
console.log(`鼠标抬起`)
}
</script>
鼠标进入(onmouseenter)和鼠标移出(onmouseleave)
鼠标在进入和移出 (div)时,会触发事件
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
#bbc{
width: 200px;
height: 200px;
background: palegreen;
}
</style>
</head>
<body>
<div id="bbc" >
</div>
</body>
<script>
var bbc = document.getElementById(`bbc`)
bbc.onmouseenter = function(){
console.log(`鼠标进入`)
}
bbc.onmouseleave= function(){
console.log(`鼠标移出`)
}
</script>
鼠标移动事件(onmousemove)
鼠标在div中移动时,会触发事件
鼠标移入(onmouseover)和移出(onmouseout)事件
鼠标在div中移出,移入时会触发事件
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
#bbc{
width: 200px;
height: 200px;
background: palegreen;
}
</style>
</head>
<body>
<div id="bbc" >
</div>
</body>
<script>
var bbc = document.getElementById(`bbc`)
bbc.onmouseover = function(){
console.log(`鼠标移入`)
}
var bbc = document.getElementById(`bbc`)
bbc.onmouseout = function(){
console.log(`鼠标移出`)
}
</script>
onclick(单击事件)
ondblclick(双击事件)
onmousedown(鼠标按下事件)
onmouseup(鼠标抬起事件)
onmouseenter (鼠标进入)
onmouseleave (鼠标离开)
onmousemove(鼠标移动)
onmouseover(鼠标移入)
onmouseout(鼠标移出)
键盘事件
键盘按键事件
当键盘在body中按下抬起时,触发事件。
<script>
document.body.onkeydown=function(event){
console.log(`键盘按键按下`)
var cc = event.keyCode
console.log(cc)
}
document.body.onkeyup = function(){
console.log(`键盘按键抬起`)
var cc = event.keyCode
console.log(cc)
}
</script>
都是获取键值,只是不同浏览器的兼容性问题导致,部分按键的键值无法获取
var code = event.keyCode || event.charCode || event.which
console.log(code)
onload页面加载
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload = function() {
foo()
var bbc = document.getElementById('bbc')
console.log(bbc)
}
function foo(){
console.log(`foo`)
}
</script>
</head>
<body>
<div id="bbc" >
</div>
</body>
滚动事件
<body>
<div id="bbc">
</div>
</body>
<script >
document.onscroll= function(){
console.log(`滚动事件`)
console.log(document.documentElement.scrollTop)
}
</script>
表单事件
<body>
<form id="form" action="" method="get">
<p>用户名:<input type="text" id="username" /></p>
<input type="submit" value="提交"/>
</form>
</body>
<script >
var username = document.getElementById('username')
username.onfocus = function(){
console.log('获取焦点')
}
</script >
窗口大小变化
<script >
window.onresize =function(){
console.log(`窗口大小变化`)
}
</script>
获取焦点,失焦
<body>
<form id="form" action="" method="get">
<p>用户名:<input type="text" id="username" /></p>
<input type="submit" value="提交"/>
</form>
</body>
<script >
var username = document.getElementById('username')
username.onfocus = function(){
console.log('获取焦点')
}
username.onblur = function(){
console.log('失焦了')
}
</script >
获取焦点,失焦,输入,触发事件
<body>
<form id="form" action="" method="get">
<p>用户名:<input type="text" id="username" /></p>
<input type="submit" value="提交"/>
</form>
</body>
<script >
var username = document.getElementById('username')
username.onfocus = function(){
console.log('获取焦点')
}
username.onblur = function(){
console.log('失焦了')
}
username.onchange = function(){
console.log('输入了')
}
username.oninput = function(){
console.log('oninput事件触发了')
}
</script>
登录
<body>
<form id="form" action="" method="get">
<p>用户名:<input type="text" id="username" /></p>
<input type="submit" value="提交"/>
</form>
</body>
<script >
var username = document.getElementById('username')
username.onfocus = function(){
console.log('获取焦点')
}
username.onblur = function(){
console.log('失焦了')
}
username.onchange = function(){
console.log('输入了')
}
username.oninput = function(){
console.log('oninput事件触发了')
}
// 表单处理函数
function login(){
alert('登录成功')
}
var form = document.getElementById('form')
form.onsubmit = function(event){
alert('登录')}
</script>