从JavaScript随机化HTML表格行 [英] Randomize HTML Table Rows from JavaScript

查看:94
本文介绍了从JavaScript随机化HTML表格行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我有html表,这个表包含10行和2列,每当用户调用此HTML页面时,我想要



随机。

我怎么能用JavaScript做这个?


等待回复

谢谢

Dear All,
I have html table and this table contains 10 Rows and 2 column, I want
every time this HTML page is called by the user to view the rows
Randomly.
How can I do this from JavaScript?

waiting for reply
Thank you

推荐答案

sanju于2008年1月1日在comp.lang.javascript写了
sanju wrote on 01 jul 2008 in comp.lang.javascript:

我有html表,这个表包含10行和2列,每当用户调用此HTML页面查看行时,我想要

/>
随机。

如何从JavaScript执行此操作?
I have html table and this table contains 10 Rows and 2 column, I want
every time this HTML page is called by the user to view the rows
Randomly.
How can I do this from JavaScript?



你可以在加载时使用dom更改行


或document.write加载页面时写入单独的行。


使用Math.random()。


不,我不打算写你的代码,去吧你自己。


-

Evertjan。

荷兰。

(请更改x 我的电子邮件地址中的点数

you can use the dom when loaded to change the lines

or document.write when loading the page to write the seperate lines.

use Math.random().

No, I am not going to write your code, have a go at it yourself.

--
Evertjan.
The Netherlands.
(Please change the x''es to dots in my emailaddress)


sanjuaécrit:
sanju a écrit :

亲爱的所有人,

我有html表,这个表包含10行和2列,每次用户调用此HTML页面查看行时,我想要



随机。

如何从JavaScript中执行此操作?
Dear All,
I have html table and this table contains 10 Rows and 2 column, I want
every time this HTML page is called by the user to view the rows
Randomly.
How can I do this from JavaScript?



你可以赚:

- 表格行的数组

< http:// www.howtocreate.co.uk/tutorials/javascript/domtables>

< http://developer.mozilla.org/en/docs/DOM:table>

< http://developer.mozilla.org/en/docs/DOM:lement.cloneNode>

- 随机数组

< http:// javascript。 about.com/library/blsort2.htm>

- 用数组'相同索引的项交换每个表的行

< http:// developer.mozilla.org/en/docs/DOM:element.replaceChild>

< script type =" text / javascript">

function tableRowsRandom(myTable) {

var T = document.getElementById(myTable);

T = T.tBodies [0];

T = T.rows;

var n = T.length,R = new Array(n);

for(var i = 0; i< n; i ++)R [i] = T [i] .cloneNode(true);

R.sort(randOrd);

for(var i = 0; i< n; i ++){

T [i] .parentNode.replaceChi ld(R [i],T [i]);

}

}

函数randOrd(){return(Math.round(的Math.random()) - 0.5); }

< / script>


demo:< http://cjoint.com/?hbkQ1POY0W>


-

sm


you can make :
- array of table''s rows
<http://www.howtocreate.co.uk/tutorials/javascript/domtables>
<http://developer.mozilla.org/en/docs/DOM:table>
<http://developer.mozilla.org/en/docs/DOM:element.cloneNode>
- random the array
<http://javascript.about.com/library/blsort2.htm>
- exchange each table''s row with the array''s item of same index
<http://developer.mozilla.org/en/docs/DOM:element.replaceChild>
<script type="text/javascript">
function tableRowsRandom(myTable) {
var T = document.getElementById(myTable);
T = T.tBodies[0];
T = T.rows;
var n = T.length, R = new Array(n);
for(var i=0; i<n; i++) R[i] = T[i].cloneNode(true);
R.sort(randOrd);
for(var i=0; i<n; i++) {
T[i].parentNode.replaceChild(R[i],T[i]);
}
}
function randOrd(){ return (Math.round(Math.random())-0.5); }
</script>

demo : <http://cjoint.com/?hbkQ1POY0W>

--
sm


sanju写道:
sanju wrote:

我有html表和这个表包含10行和2列,我希望

每次用户调用这个HTML页面来查看行

随机。

如何从JavaScript执行此操作?
I have html table and this table contains 10 Rows and 2 column, I want
every time this HTML page is called by the user to view the rows
Randomly.
How can I do this from JavaScript?



快速入侵:


/ **

* @return伪随机IEEE间隔为-754双倍

*< tt> [0,1)< / tt> ;.

* @type number

* /

函数prng()

{

var r = Math.random();


// Opera bug解决方法

如果(r == 1)r = 0;


返回r;

}


/ **

*返回区间内的伪随机整数值

*< tt> [0,顶部)< / tt>,或在区间< tt> [bottom,top]< / tt>

*如果< code>底部< /代码已提供。

*

* @param顶部:数字

* @param底部:可选数字

* @return伪随机整数值在指定的时间间隔内

* @type number

* /

函数prng_int(顶部,底部)

{

如果(!bottom)bottom = 0;

返回Math.floor(prng()*(top - bottom))+底部;

}


/ **

*随机对文档第一个表体的行进行排序。 />
* /

函数bodyLoad()

{

//获取表对象引用

var t = ...

if(t)

{

//得到表体对象参考

var tbody =(t.tBodies || [])[0];

if(tbody)

{

for(var rows = tbody.rows,len = rows.length ,i = len; i--;)

{

tbody.insertBefore(rows [i],rows [prng_int(len)]);

}

}

}

}


< body onload =" bodyLoad()">

Quick hack:

/**
* @return A pseudo-random IEEE-754 double in the interval
* <tt>[0, 1)</tt>.
* @type number
*/
function prng()
{
var r = Math.random();

// Opera bug workaround
if (r == 1) r = 0;

return r;
}

/**
* Returns a pseudo-random integer value in the interval
* <tt>[0, top)</tt>, or in the interval <tt>[bottom, top)</tt>
* if <code>bottom</codewas provided.
*
* @param top : number
* @param bottom : optional number
* @return pseudo-random integer value in the specified interval
* @type number
*/
function prng_int(top, bottom)
{
if (!bottom) bottom = 0;
return Math.floor(prng() * (top - bottom)) + bottom;
}

/**
* Sorts the rows of the first table body of the document randomly.
*/
function bodyLoad()
{
// get table object reference
var t = ...
if (t)
{
// get table body object reference
var tbody = (t.tBodies || [])[0];
if (tbody)
{
for (var rows = tbody.rows, len = rows.length, i = len; i--;)
{
tbody.insertBefore(rows[i], rows[prng_int(len)]);
}
}
}
}

<body onload="bodyLoad()">


等待回复
waiting for reply



Usenet不是一个权利。我们[tm]处理你的问题,因为它看起来很有意思地花费我们[tm]的时间,而不是因为解决方案

对你来说很紧急。对你来说要求越高,我们就越不感兴趣了。[/ b]。


< http://catb.org/~esr/ faqs / smart-questions.html>

PointedEars

-

使用任何版本的Microsoft Frontpage创建您的网站。

(这不会阻止人们查看你的来源,但是没有人

会想要偷它。)

- 来自< http: //www.vortex-webdesign.com/help/hidesource.htm>

Usenet is not a right. We[tm] deal with your problem because it looks
interesting enough to spend our[tm] time with, not because the solution
is urgent for you. The more demanding you are, the less interesting it
becomes for us[tm].

<http://catb.org/~esr/faqs/smart-questions.html>
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won''t prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>


这篇关于从JavaScript随机化HTML表格行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆