我来解释一下这个函数,呵呵 bind 这个函数用来将参数和函数绑定后返回。 Function.prototype.bind = function(i);表示给Function这个类型增加一个扩展功能函数。delBtn.onclick = f.bind(i);这句首先执行f.bind()这个函数(bind已经在前面被声明了)。bind方法返回了一个函数句柄。 我这个说法可能很有问题。总之返回了一个函数 return function(){ self.apply(null,arg); }右边执行完了,句柄值被赋予了左边的onclick事件。apply怎么解释?翻开dhtml手册,还是不知道这个函数做什么用Once the apply method is called, changes to the object's properties are not displayed until you call the play method to start the transition.
onclick="removeImage('+n+');"n是动态改变的。
<body>
<div id="xxx"></div>
</body>
</html>
<SCRIPT LANGUAGE="JavaScript">
<!--
var delBtn = document.createElement('input');
delBtn.type = 'button';
delBtn.onclick = xx;
delBtn.value='delete';document.getElementById("xxx").appendChild(delBtn);function xx(){
alert(this.value);
}
//-->
</SCRIPT>ie6和firefox下都是通用的
不执行,firefox报错。
这个不行,要包装一下,代码已经给了
可以。
可是参数怎么带过去?
<script language="javascript">
<!--
Function.prototype.bind = function(){
var self = this;
var arg = arguments;
return function(){
self.apply(null,arg);
}
}
for(var i=0;i<5;i++)
{
var delBtn = document.createElement( 'input' );
delBtn.type = 'button';
var f = function(i){
alert(i);
}
delBtn.onclick = f.bind(i);
delBtn.value='delete';
document.body.appendChild( delBtn );
}
//-->
</script>
又用了稻草人兄的函数,也解决了问题。的确是非一流高手不能为阿,佩服佩服。
揭帖。
bind 这个函数用来将参数和函数绑定后返回。
Function.prototype.bind = function(i);表示给Function这个类型增加一个扩展功能函数。delBtn.onclick = f.bind(i);这句首先执行f.bind()这个函数(bind已经在前面被声明了)。bind方法返回了一个函数句柄。
我这个说法可能很有问题。总之返回了一个函数
return function(){
self.apply(null,arg);
}右边执行完了,句柄值被赋予了左边的onclick事件。apply怎么解释?翻开dhtml手册,还是不知道这个函数做什么用Once the apply method is called, changes to the object's properties are not displayed until you call the play method to start the transition.