zwq 2011-04-18
Asmanyofyoumusthavereadorrealised(idid)whileworkingwithit,MSInternetExplorerdoesn'tallowyoutosettheinnerHTMLpropertyofanytablerelatedtag(table,thead,tbody,trexceptfortd).Itsays"UnknownRuntimeException".Youareprovidedwithexplicitmethodstohandlethis.LikecreateRow,createCellandstuff...AlsoyoucanuseW3CDOMappendchild.
Goodwaytomaketables,butwhatifIhavetheTableHeaddefinedintheHTMLfileandreadtheTableBodyfromaXMLfileaswelldefinedstring.Isimplywanttohavethisinthetable...OnewayisIparsethestringasXMLandloopthroughthenodescreatingthecellsandsoon(thatiswhatIfirstdid),Butwhydoweneedthatunecessaryprocessing.Wedon'thaveanymethodtoparsethestringandgetintoourDOMdirectly.
TheworkaroundIworkedoutisprobablynottheonlyorbest,butworksgood.
supposewehaveavariablesXmlBodywhichhasallthetablebodyrowsdefinedlike:
varsXmlBody="<tr><td>cellOne1</td><td>cellTwo1</td><tr>"+
"<tr><td>cellOne2</td><td>cellTwo2</td><tr>";
Ofcourseitwillbecreateddynamically;)
NowtoaddthistotheDOMencloseitinactualtableandtbodytags.
sXmlBody="<table><tbody>"+sXmlBody+"</tbody></table>";
andgetitinDOMparsedasHTMLusingatempdiv
vartempDiv=document.createElement("DIV");
tempDiv.innerHTML=sXmlBody;
NowIcanuseW3CDOMtoappendthechildfromthetempdivtothetableinourdocument.ConsidertheidofthetableintheHTMLdocumentidtableResults
vartableElement=document.getElementById("tableResult");
tableElement.appendChild(tempDiv.firstChild.firstChild);
Ifyouaregoingtoreloadthetablecontent,itwillbeusefultohaveanidforthetbodytaganduseremoveChildonthedocumentstablewiththetbodyelementasparameter.
Ihopethat'shelpfultoyouall,anditworksfineinFirefoxtoo!
TillNexttime...Happycoding
表格的现在还是较为常用的一种标签,但不是用来布局,常见处理、显示表格式数据。在HTML网页中,要想创建表格,就需要使用表格相关的标签。<table> <tr> <td>单元格内的文字</td> ...