【问题标题】:Javascript array write html tableJavascript数组写html表
【发布时间】:2013-03-19 18:30:48
【问题描述】:

我正在尝试创建一个像这样的表: 图片在这里:http://speedy.sh/JahdC/Screen-Shot-2013-03-28-at-8.12.08-PM.png

___________________________________
|function|X|Y|function(x,y)       |
|x+y |rndX|rndY|Result of function|
|x+1 | ""  ""  ""
|x/y | "" "" "" 
|x%y
|square root(x^2 + y^2)
|x^2
|10*x + y/2
|SUM of all results

我目前拥有的代码是: http://jsfiddle.net/wMF5M/10/

<table id="content" border="1"></table>

function RandX() {
var x = Math.floor(Math.random() * 300);
return x;
}

function RandY() {
var y = Math.floor(Math.random() * 300);
return y;
}

var math1 = function (x, y) {
return x * y;
};
var math2 = function (x, y) {
return x + 1;
};
var math3 = function (x, y) {
return x / y;
};
var math4 = function (x, y) {
return x % y;
};
var math5 = function (x, y) {
return math.sqrt(x ^ 2 + y ^ 2);
};
var math6 = function (x, y) {
return x ^ 2;
};
var math7 = function (x, y) {
return (10 * x) + (y / 2);
};
var math8 = function (x, y) {
return ((x * y) + (x + 1) + (x / y) + (x % y) + 
(math.sqrt(x ^ 2 + y ^ 2)) + (x ^ 2) + ((10 * x) + (y / 2)));
};
// the function above is supposed to add the results of all functions together, 
and push into the array to be displayed
var maths = [];

maths.push(math1);
maths.push(math2);
maths.push(math3);
maths.push(math4);
maths.push(math5);
maths.push(math6);
maths.push(math7);
maths.push(math8);
//maths[0](RandX,2);//return RandX* RandY
//maths[1](3,4);//12 - put random numbners here?



var content = document.getElementById("content");
var tr = document.createElement("tr");
for (var i = 0; i < maths.length; i++) {

    var td = document.createElement('td');
    td.innerHTML = +maths[0](RandX(), RandY());
    tr.appendChild(td);
}
content.appendChild(tr);

使用这种方法我如何写表格的第一行,保持这种格式?

【问题讨论】:

  • 您的输出格式与图片完全不同。你到底想要什么?

标签: javascript html arrays function random


【解决方案1】:

如果您要动态地逐行填充表格,请将代码放在表格标签中,如下所示...

<table>

----start code to write row
----<tr><td> $result </td></tr>
----loop
----end of code

</table>

【讨论】:

    【解决方案2】:

    您可以使用一些在线 LaTeX 方程生成器从纯 LaTeX 制作方程图像。例如,http://latex.codecogs.com/gif.latex

    检查此jsFiddle。它更接近图像格式。

    【讨论】:

      猜你喜欢
      • 2015-01-13
      • 2017-10-02
      • 2015-08-08
      • 2011-03-22
      • 2012-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-16
      相关资源
      最近更新 更多