分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

原生ajax动态添加数据

发布时间:2023-09-06 02:00责任编辑:白小东关键词:暂无标签
<!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,url,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‘,‘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; ???????????????}); ???????????},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);

原生ajax动态添加数据

原文地址:https://www.cnblogs.com/moon-yyl/p/9210788.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved