Heim > Artikel > Backend-Entwicklung > PHP verwendet js, um Tabellen zu sortieren
<!DOCTYPE> <html> <Kopf> <meta http-equiv="Content-type" c charset="utf-8"> <title><span>sortieren</span> Tabelle</title> <Stil> *<span>{ Rand</span>:<span>0px; Polsterung</span>:<span>0px; } Körper{ Hintergrund</span>:<span>#</span><span>ccc;</span><span> } Tisch{ Breite</span>:<span>350px; margin</span>:0<span> auto; Hintergrund</span>-Farbe:<span>#</span><span>eee;</span><span> } Tabelle th{ Cursor</span>:<span>Hand; padding</span>:5px 0<span>; Hintergrund</span>-Farbe:<span>#</span><span>999;</span><span> } Tabelle td{ Hintergrund</span>-Farbe:<span>#</span><span>fff;</span> Schriftgröße:<span>16px; Schriftart</span>-weight:<span>normal; text</span>-align:<span>center; Linie</span>-height:<span>30px; } </span></style> <script language="javascript"> <span>Funktion</span><span> sortCells(type){ </span><span>var</span> tbs=document.getElementsByTagName("table")[0<span>]; </span><span>var</span> arr=<span>[]; </span><span>var</span> arr2=<span>[]; </span><span>for</span>(<span>var</span> i=1;i<tbs.rows.length;i <span>){ </span><span>var</span> text=tbs.rows[i].cells[type].<span>innerText; arr</span>.<span>push(text); arr2[text]</span>=<span>i; } </span><span>if</span>(type==0<span>){ arr</span>.<span>sort</span>(<span>function</span>(a,b){<span>return</span> a-<span>b}); }</span><span>sonst</span><span>{ arr</span>.<span>sort</span><span>(); } </span><span>var</span> temp=""<span>; </span><span>for</span>(<span>var</span> j=1;j<tbs.rows.length;j <span>){ temp</span>=tbs.rows[j].cells[0].<span>innerText; tbs</span>.rows[j].cells[0].innerText=tbs.rows[arr2[arr[j-1]]].cells[0].<span>innerText; tbs</span>.rows[arr2[arr[j-1]]].cells[0].innerText=<span>temp; temp</span>=tbs.rows[j].cells[1].<span>innerText; tbs</span>.rows[j].cells[1].innerText=tbs.rows[arr2[arr[j-1]]].cells[1].<span>innerText; tbs</span>.rows[arr2[arr[j-1]]].cells[1].innerText=<span>temp; temp</span>=tbs.rows[j].cells[2].<span>innerText; tbs</span>.rows[j].cells[2].innerText=tbs.rows[arr2[arr[j-1]]].cells[2].<span>innerText; tbs</span>.rows[arr2[arr[j-1]]].cells[2].innerText=<span>temp; </span><span>//</span><span> console.log(arr2);</span><span>for</span>(<span>var</span> i=1;i<tbs.rows.length ;ich <span>){ </span><span>var</span> text=tbs.rows[i].cells[type].<span>innerText; arr2[text]</span>=<span>i; } } } </span></script> </head> <Körper> <center><span>sortieren</span> Tabelle</center> <table border="0"> <tr> <th alt="" /> <p> 以上就介绍了php使用js对表格进行排序, 包括了方面的内容, 希望对PHP教程有兴趣的朋友有所帮助。</p> <p> </p>