<html>
<head>
<style type="text/css">
.test {
width:100px;
height:100px;
border:red 1px solid;
}
</style>
</head>
<body>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
</body>
</html>
<script type="text/javascript">
var k = document.getElementsByTagName("div");
for(var i = 0; i < k.length; i ++) {
(function(){
k[arguments[0]].onclick = function() {
alert(arguments[0]);
}
})(i)
}
</script>
匿名函数不写形参而用arguments代替就不行,请问下这个是为什么?在此先谢过了
<head>
<style type="text/css">
.test {
width:100px;
height:100px;
border:red 1px solid;
}
</style>
</head>
<body>
<div class="test"></div>
<div class="test"></div>
<div class="test"></div>
</body>
</html>
<script type="text/javascript">
var k = document.getElementsByTagName("div");
for(var i = 0; i < k.length; i ++) {
(function(){
k[arguments[0]].onclick = function() {
alert(arguments[0]);
}
})(i)
}
</script>
匿名函数不写形参而用arguments代替就不行,请问下这个是为什么?在此先谢过了
(function fn1(){
k[arguments[0]].onclick = function fn2() {
alert(arguments[0]); //你这里的arguments[0]是fn2的参数,所以肯定不行啊
}
})(i)
}
要改成
for(var i = 0; i < k.length; i ++) {
(function (){
var num = arguments[0];
k[num ].onclick = function () {
alert(num);
}
})(i)
}
for(var i = 0; i < k.length; i ++) {
k[i].onclick = (function(n) {
return function(){
alert(n);//n就是传过来的i
}
})(i)
}
k[arguments[0]].onclick = function() { //这个arguments指的是上边的function的参数
alert(arguments[0]);//这个arguments指的是onclick的function中的参数 二者是不一样的
}
})(i)
for(var i = 0; i < k.length; i ++) {
(function(){
k[arguments[0]].onclick = function() {
alert(arguments[0]);
}
})(i)
}
两个arguments[0]并不是同一个。。
arguments // 我是A的arguments对象.
function B(){
arguments //我是B的arguments对象
}
}