当前位置: 首页 > 图文教程 > 网络编程 > Javascript > JScript 寫 sortNode

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 JScript 寫 sortNode


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-10   浏览: 334 ::
收藏到网摘: n/a

JScript 利用 Array.sort() 和 Nodes.swap()
來完成重新排列。

但速度比XML的sortNode慢。(-_-メ)

 

 

 

 

 

<!--
http://dwin.net

http://dewin.tk

Copyright(c) 1998-2004 dewin all rights reserved

Start 2003-09-18 15:50
Finish  2003-09-18 16:17
-->

<body>
<script>
/*  create the table   */
TRlength = 100

letter="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
str = "\
<table id=test style=''>\
 <tbody>\
  <tr onclick='if(event.srcElement!=this)sortNode(event.srcElement)'><td style='background:#FF69B4'>Letter</td><td style='background:#ADFF2F'>Name</td><td style='background:#87CEFA'>Money</td></tr>\
 </tbody>\
 <tbody>"
for(i=0;i<TRlength;i++)
 str += "<tr><td>"+letter.charAt(Math.floor(Math.random()*10000%62))+"</td><td>"+String.fromCharCode(Math.round(Math.random()*(40869-19968)+19968))+"</td><td>"+String(Math.random()).slice(2)+"</td></tr>"
str += "</tbody>\
</table>"
document.write(str)

 

 

 

 

ary = [];
oTR = test.childNodes[1].childNodes;
oTRorder = test.firstChild.firstChild.childNodes;

for(i=0, j=oTR.length; i<j; i++)//collect TR nodes in Array
 ary[i] = oTR[i];
for(i=0, j=oTRorder.length; i<j; i++)
 oTRorder[i].order = -1;

 

function sortNode(SortNode){
var time = new Date()
var tempa, tempb, oTD, orders
oTD = SortNode.sourceIndex-SortNode.parentElement.sourceIndex-1;//which TD we Click
orders = SortNode.order = -SortNode.order;

ary.sort(//sort the nodes here.
 function(a,b){
  tempa = a.childNodes[oTD].innerText;//you can use   a.childNodes[oTD].innerText.length   or other things to sort
  tempb = b.childNodes[oTD].innerText;
  temp = tempa<tempb?-orders:tempa==tempb?0:orders;
  return temp;
  }
 )

for(i=0; i<oTR.length; i++)
 oTR[i].swapNode(ary[i]);//swap TR nodes

alert(new Date()-time);
}

</script>