用下面的方法传递参数是可以
function outerJS(obj) {
alert(obj.id);
(function(obj) {
alert(obj.outerHTML);
})(obj);
}但是如果是运用在AJAX的xmlHttp.onreadystatechange = (function(obj){
if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
var xxx = obj.parentNode;
alert(xxx.outerHTML);
}
})(obj);这样的话会提示尚未实现
如果函数体很长,当然可以写成另一个函数而不用匿名函数
但像这种函数体只有那么一点,但是又一定要传递参数的,有办法用匿名函数来实现吗?
function outerJS(obj) {
alert(obj.id);
(function(obj) {
alert(obj.outerHTML);
})(obj);
}但是如果是运用在AJAX的xmlHttp.onreadystatechange = (function(obj){
if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
var xxx = obj.parentNode;
alert(xxx.outerHTML);
}
})(obj);这样的话会提示尚未实现
如果函数体很长,当然可以写成另一个函数而不用匿名函数
但像这种函数体只有那么一点,但是又一定要传递参数的,有办法用匿名函数来实现吗?
对这个事件来说,只是通知你XMLHTTP对象的状态发生改变,由你在这个事件处理函数中去取来XMLHTTP所取到的数据。根本不需要也不允许设什么参数。
从你的处理函数中的代码来看,也没有什么实际意义,obj和parentNode这些,用在这里都不对。
看下一般的匿名式处理代码:
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.myForm.time.value=xmlHttp.responseText;
//若想操纵什么xxx.outerHTML,就在这里getElementById
}
}
xmlHttp.onreadystatechange = (function(obj){
return function(){
if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
var xxx = obj.parentNode;
alert(xxx.outerHTML);
}
}
})(obj);