发布网友 发布时间:2022-04-22 15:24
共1个回答
热心网友 时间:2024-03-08 05:19
不知是否符合你的要求,以IE浏览器为例,仅供参考:
var ajaxobject; //定义一个全局变量,存放XMLHttpRequest
function getPic (cat) {
try {
ajaxobject = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e) {
ajaxobject = new ActiveXObject("Msxml2.XMLHTTP");
}
ajaxobject.onreadystatechange = function () {
changeStateFunction(cat);
}
var submitUrl = ''; //创建一个url,我就省略了。
ajaxobject.open('GET', submitUrl, true);
ajaxobject.send(null);
var elem = document.getElementById(cat);
elem.innerHTML = "Loading";
}
function changeStateFunction (cat) {
if (ajaxobject.readyState == 4) {
if (ajaxobject.status == 200) {
parseData(cat);
}
}
}
var name = [];
var src = [];
var desc = [];
var header;
function parseData (cat) {
var xmlData = ajaxobject.responseXML;
header = ajaxobject.getResponseHeader();
var pics = xmlData.getElementByTagName("item");
for (var i = 0; i < pics.length; i++) { //循环所有元素
name[i] = pics[i].getElementByTagName("name")[0].firstChild.nodeValue;
src[i] = pics[i].getElementByTagName("url")[0].firstChild.nodeValue;
desc[i] = pics[i].getElementByTagName("desc")[0].firstChild.nodeValue;
}
var result = "";
for (var i = 0; i < name.length; i++) { //把数组显示成html
result += '<a href=""><img src="images/' + src[i] + '"alt = "' + desc[i] + '">' + name[i] + '</a>';
}
var elem = document.getElementById(cat);
elem.innerHTML = result;
}
其中header已经取出了,我没作处理而已。