点击事件
1-1.onclick
element.onclick = function(){//执行code}
<body><ul><li>html </li><li>css </li><li>javascript </li></ul><script>var lis = document.getElementsByTagName("li");// console.log(lis)// lis.onclick = function()// {// console.log(1)// }for( var i=0;i<lis.length;i++){lis[i].onclick = function(){console.log(this)this.style.display ="none";}}</script></body>
1-2 onfocus/onblur(获取/失去焦点)
<body><input type="text" id="test"><script>var test = document.getElementById("test");test.onfocus = function(){this.style.backgroundColor ="red"}test.onblur = function(){this.style.backgroundColor ="#fff"}</script></body>
1-3 onmouseover/onmouseout(鼠标悬停/移开)
<body><p id="p">hello world</p><script>var p =document.getElementById("p");p.onmouseover = function(){this.style.backgroundColor = "red"}p.onmouseout =function(){this.style.backgroundColor = "yellow"}</script></body>
window相关事件
2-1onload
window.onload 页面在加载完毕之后才会触发
<script src="js/index.js"></script><p id="p">hello world</p>window.onload = function(){var p =document.getElementById("p");console.log(p)}
2-2onresize 窗口大小改变的时候触发
<script >window.onresize = function(){console.log("窗口改变了")}</script>
2-3onscroll 窗口滚动时触发
<body><div></div><script>window.onscroll = function () {console.log(1)}</script></body>
onchange 输入框内容改变时触发
4-1
<input type="text" id="input"><script>var input = document.getElementById("input");input.onchange = function(){console.log("hello")}</script>
键盘事件与KeyCode属性
event.key 获取键盘按下对应的键 event.keyCode 获取按下的键的状态码
4-1onkeydown(用户按下键盘时触发)
<body><input type="text" id="app"><script>var app = document.getElementById("app");app.onkeydown = function(event){console.log(event)console.log(event.key)console.log(event.keyCode)}</script></body>
<body><input type="text" id="app"><script>var app = document.getElementById("app");app.onkeydown = function(event){console.log(event.keyCode)if(event.keyCode==13){ ----Enter键的keyCode==13console.log(this.value)}}</script></body>
4-2 onkeypress 在键盘按键按下并释放一个键时发生
<input type="text" id="app"><script>var app = document.getElementById("app");app.onkeypress = function(event){console.log(event)}</script>
4-3 onkeyup 在键盘按键松开时发生
<input type="text" id="app"><script>var app = document.getElementById("app");app.onkeyup = function(event){console.log(event)}</script>
内联事件
<body><button onclick="go()">btn</button><script>function go(){console.log("hello world")}</script></body>
