微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

原生ajax动态添加数据

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<Meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>标题文档</title>
<script>

    function ajax(method,url,date,success){
        
        var xhr = null;
        try{
            xhr = new XMLHttpRequest(); 
        }catch(e){
            xhr = new ActiveXObject(Microsoft.XMLHTTP);
        }        
        if(method == get && date){
            url += ?+date;
        }
        xhr.open(method,true);
        
        if(method == get){
            xhr.send();
        }else{
            xhr.setRequestHeader(content-type,application/x-www-form-urlencoded);
            xhr.send(date);
        }
        
        xhr.onreadystatechange = function(){
            if(xhr.readyState == 4){
                if(xhr.status == 200){
                    success && success(xhr.responseText);
                }else{
                        alert(出错了+xhr.status);
                }
            }
        }
    }

    
    window.onload = function(){
        var btn = document.getElementById(btn);
        btn.onclick = function(){
            ajax(get,getNews.PHP,‘‘,function(date){
                    var date = JSON.parse(date);
                    var oUl = document.getElementById(list);
                    var value = ‘‘;
                    for(var i=0;i<date.length;i++){
                        value += <li><a href="#">+date[i].title+</a>&nbsp;<span>+date[i].date+</span></li>
                    }
                    oUl.innerHTML = value;
            });
            var timer = null;
            clearInterval(timer);
            timer = setInterval(function(){
                ajax(get,function(date){
                    var date = JSON.parse(date);
                    var oUl = document.getElementById(list);
                    var value = ‘‘;
                    for(var i=0;i<date.length;i++){
                        value += <li><a href="#">+date[i].title+</a>&nbsp;<span>+date[i].date+</span></li>
                    }
                    oUl.innerHTML = value;
                });
            },3000);
        };
        
    };
</script>
</head>

<body>
    <input type="button" value="按钮" id="btn"/>
    <ul id="list"></ul>
</body>
</html>
<?PHP
    header(‘content-type:text/html; charset="utf-8"‘);
    error_reporting(0);
    
    $news = array(
            array(‘title‘=>‘12121212121牌‘,‘date‘=>‘2018-6-8‘),array(‘title‘=>‘美又打2000亿关税牌‘,‘date‘=>‘2018-6-1‘),array(‘title‘=>‘北京平均可月省约150‘,‘date‘=>‘2018-6-2‘),array(‘title‘=>‘个税起征点提至5000‘,‘date‘=>‘2018-6-3‘),array(‘title‘=>‘美又打2000亿关税‘,‘date‘=>‘2018-6-4‘),array(‘title‘=>‘哥伦比亚vs日本:J罗‘,‘date‘=>‘2018-6-5‘),array(‘title‘=>‘北京时间"一粽浓情"活动结束‘,‘date‘=>‘2018-6-6‘),
        );
    echo json_encode($news);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐