加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

原生ajax动态添加数据

发布时间:2020-12-16 03:04:04 所属栏目:百科 来源:网络整理
导读:! 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" /
<!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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读