javascript 事件返回值的传递问题

最近在学习AJAX,遇到这样一个问题。
在一个函数中,需要触发一个事件,并且这个事件上要绑定一个函数,代码如下

function ajax(target,customquery,ajaxstr){
var myajax=new XMLHttpRequest();
myajax.open("get",decodeURI(target+"?t="+new Date().getTime()+"&"+customquery),true);
myajax.send();
myajax.onreadystatechange=function(){
if(myajax.readyState==4){
if(myajax.status==200){ 
ajaxstr=myajax.responseText;
return ajaxstr;
}
}
}
}

在myajax.onreadystatechange这里绑定的函数返回一个值,但是我不知道该怎么去获取它。

最终的目的是直接调用ajax函数,就可以获取到返回的字符串,感谢大家来关注一下吧~

肥皂起泡泡
浏览 597回答 2
2回答

互换的青春

12345678910111213141516171819202122232425262728293031<html><head><script&nbsp;type="text/javascript">function loadXMLDoc(){var xmlhttp;if (window.XMLHttpRequest)&nbsp;&nbsp;{// code for &nbsp; IE7+, Firefox, Chrome, Opera, Safari&nbsp;&nbsp;xmlhttp=new &nbsp; XMLHttpRequest();&nbsp;&nbsp;}else&nbsp;&nbsp;{// code for &nbsp; IE6, IE5&nbsp;&nbsp;xmlhttp=new &nbsp; ActiveXObject("Microsoft.XMLHTTP");&nbsp;&nbsp;}xmlhttp.onreadystatechange=function()&nbsp;&nbsp;{&nbsp;&nbsp;if &nbsp; (xmlhttp.readyState==4 && xmlhttp.status==200)&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;document.getElementById("myDiv").innerHTML=xmlhttp.responseText;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;}xmlhttp.open("GET","/ajax/test1.txt",true);xmlhttp.send();}</script></head><body><div&nbsp;id="myDiv"><h2>Let &nbsp; AJAX change this text</h2></div><button&nbsp;type="button"&nbsp;onclick="loadXMLDoc()">通过 AJAX 改变内容</button></body></html>&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript