为了账号安全,请及时绑定邮箱和手机立即绑定

javascript 事件返回值的传递问题

javascript 事件返回值的传递问题

肥皂起泡泡 2019-03-22 15:14:30
最近在学习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函数,就可以获取到返回的字符串,感谢大家来关注一下吧~
查看完整描述

2 回答

?
互换的青春

TA贡献1797条经验 获得超6个赞


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

<html>

<head>

<script type="text/javascript">

function loadXMLDoc()

{

var xmlhttp;

if (window.XMLHttpRequest)

  {// code for   IE7+, Firefox, Chrome, Opera, Safari

  xmlhttp=new   XMLHttpRequest();

  }

else

  {// code for   IE6, IE5

  xmlhttp=new   ActiveXObject("Microsoft.XMLHTTP");

  }

xmlhttp.onreadystatechange=function()

  {

  if   (xmlhttp.readyState==4 && xmlhttp.status==200)

    {

    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

    }

  }

xmlhttp.open("GET","/ajax/test1.txt",true);

xmlhttp.send();

}

</script>

</head>

<body>

<div id="myDiv"><h2>Let   AJAX change this text</h2></div>

<button type="button" onclick="loadXMLDoc()">通过 AJAX 改变内容</button>

</body>

</html>


 


查看完整回答
反对 回复 2019-03-26
  • 2 回答
  • 0 关注
  • 594 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信