代码 1
<!DOCTYPE HTML>
<html>
<body>
<link type="text/css" rel="stylesheet" href="example.css">
<div class="d1">1
<div class="d2">2
<div class="d3">3
</div>
</div>
</div>
<script>
var divs = document.getElementsByTagName('div')
for(var i=0; i<divs.length; i++) {
divs[i].onclick = function(e) {
e = e || event
var target = e.target || e.srcElement
this.style.backgroundColor='yellow'
alert("target = "+target.className+", this="+this.className)
this.style.backgroundColor = ''
}
}
</script>
</body>
</html>
代码 2
<html>
<body onkeypress = "show_key(event.which)">
<form method="post" name="my_form">
The key you pressed was:
<input type="text" name="key_display" size="2"/>
</form>
<script type="text/javascript">
function show_key ( the_key )
{console.log(the_key);
document.my_form.key_display.value = String.fromCharCode ( the_key );
}
</script>
</body>
</html>
问题:
1.在代码1中,即使我更改function(e) {e = e || event
为function(a) { e = a || event
,它仍然有效,但是在代码2中,如果我更改show_key(event.which)
为show_key(e.which)
,它不起作用,为什么?
2.我经常看到function(e){}
还是function(event)
在js里,都是一样的吗?我可以这样使用:function(a){}
还是function(b){}
?