问题描述
function getData(){var url = "Show.do?dh=1";createXmlHttpReq();xmlHttp.onreadystatechange = function(){if(xmlHttp.readyState == 4 && xmlHttp.status == 200){var resp = xmlHttp.responseText;Convert(resp);}}xmlHttp.open("get",url,true);xmlHttp.send(null);}function Convert(resp){var mtb = document.getElementById("my_tb");var par = resp.parseJSON();var my_tb = document.getElementById("my_tb");for(var i = 0 ; i < par.length ; i ++){//alert(par[i].cate_id+"t"+par[i].cate_name+"t"+par[i].ori_price);var tbl = document.createElement("table");var tbody = document.createElement("tbody");var tr = document.createElement("tr");var td = document.createElement("td");td.setAttribute("vAlign","top");td.setAttribute("width","90");td.setAttribute("height","80");td.innerHTML = "<A href="+par[i].img_path+" target=_blank><IMG height=80 alt=点击图片查看内容 src="+par[i].img_path+" width=80 border=0></A>";var td2 = document.createElement("td");td2.setAttribute("vAlign","top");var tb = document.createElement("table");tb.setAttribute("cellSpacing","1");tb.setAttribute("cellPadding","0");tb.setAttribute("width","100%");tb.setAttribute("align","center");tb.setAttribute("border","0");var body = document.createElement("tbody");var tr2 = document.createElement("tr");var td3 = document.createElement("td");td3.innerHTML = "<A href=# target=_blank><STRONG>"+par[i].cate_name+"</STRONG></A>";var tr3 = document.createElement("tr");var td4 = document.createElement("td");td4.setAttribute("height","21");td4.innerHTML = "<FONT color=#ff0000>现价:人民币"+par[i].cur_price+"元</FONT><BR><a href=#>"+par[i].descript+"</a>!";tr2.appendChild(td3);tr3.appendChild(td4);body.appendChild(tr2);body.appendChild(tr3);tb.appendChild(body);td2.appendChild(tb);tr.appendChild(td);tr.appendChild(td2);tbody.appendChild(tr);tbl.appendChild(tbody);mtb.appendChild(tbl); } alert("完成");}<TABLE cellSpacing=0 cellPadding=0 width=776 align=center border=0 id = "my_tb"></TABLE>这代码有问题吗,为什么运行至 alert("完成") 消息弹出, 网页仍然没有任何字迹出现,求老哥们帮忙看看。 问题补充:dieslrae 写道
解决方案
在循环前alert(par.length)看看是否有数据。如果有的话将你生成的html按照逻辑打印一份出来,然后看看html是否拼错。尤其是appendChild的时候。还在忙不帮你看了。
解决方案二:
var par = resp.parseJSON();parseJSON如果我没记错这是火狐才有方法,老老实实的用eval吧var par = eval("(" + resp + ")")//如果resp是数组就不用+()了