我遇到了问题。我一直在试图解决如何避免使用全局变量的问题。我决定改用一个带有功能的对象,并相应地编写一个脚本。然而,由于预期该脚本将不会运行:访问在AJAX功能中设置其值的变量
GetXML: function() {
$.ajax({
type: "GET",
url: "questions.xml",
dataType: "xml",
success: function(xml) {
this.xml=xml;
$(window).trigger("canUseXML");
var docLength = $(xml).find('Question').length + 1;
alert("this really is an '" + this.xml + "' and the actual doclength is: " + docLength)//This is the first alert I mention.
} //close success
});//close AJAX
alert("yes! this is still a: " +this.xml) //This is the second alert I mention.
},
如果都跑了预期,这两个警报将导致“对象的XMLDocument”(在这一行的其他地方声明的变量:this.xml = null;
)。第一次按预期运行。但是,该函数之外的第二个函数返回值“null”。为什么?
Thanks,Elliot Bonneville
P.S.我一直被困在函数间传递变量一周的看似简单的问题上。
请用任何您正在使用的语言对本标记进行标记 – 2010-11-18 23:02:59
语言是什么?你应该把它放在标签中来说明。 – Dima 2010-11-18 23:03:50
噢,对不起。 – 2010-11-18 23:11:24