玩笑过后

导航

Ajax

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <title>Ajax</title>
  <script>
    $(function(){
        $('btn').onclick = function(){
            //创建ajax对象
            //ie6以下:new ActiveXObject('Microsoft.XMLHTTP');
            var xhr = null;
            try{
                xhr = new XMLHttpRequest();
            }catch (e){
                xhr = new ActiveXObject('Microsoft.XMLHTTP');
            }
            //发送方式
            xhr.open('post','1.txt',true);
            //接受
            xhr.send();
            //触发
            xhr.onreadystatechange = function(){
                console.log( this );
                //判断状态
                if (xhr.readyState == 4)
                {
                    alert( xhr.responseURL );
                }
            }
        }
    })
    function $(v){
        if (typeof v === 'function')
        {
            window.onload = v;
        }else if (typeof v === 'string')
        {
            return document.getElementById(v);
        }else if (typeof v === 'object')
        {
            return v
        }
    }
  </script>
 </head>
 <body>
    <input type='button' value='按钮' id='btn'/>
 </body>
</html>

 

posted on 2018-10-18 20:53  玩笑过后  阅读(105)  评论(0编辑  收藏  举报