绑定事件的两种方式
直接标签绑定<p onclick='fun();'></p>
先获取Dom对象,然后进行绑定
document.getElementById('i1').onclick=function(){
//this.style.backgroundColor = 'red';
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1" width="300px">
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>1</td><td>2</td><td>3</td></tr>
</table>
<script>
var myTrs = document.getElementsByTagName('tr')
for(var i=0;i<myTrs.length;i++){
myTrs[i].onmouseover = function () {
this.style.backgroundColor = "red";
}
myTrs[i].onmouseout = function () {
this.style.backgroundColor = '';
}
}
</script>
</body>
</html>