这就是我想做的。希望这不会太难。
我需要创建一个表,每个 td 内都有一个 div,该表是通过单击按钮创建的...例如
请选择表中的行数
请选择表格中的列数。
结果:
所以如果你点击4 和 4 它将创建一个表 4 X 4。如果您单击 3 X 1,您将创建一个表 3 X 1,等等...
任何帮助将不胜感激!
这是我正在尝试做的一些事情。我还在看你们的所有评论!
http://jsfiddle.net/irocmon/7WD8v/
我知道我需要添加Javascript如何通过id获取元素。 <代码>
Here's what I want to do. Hopefully it's not too hard.
I need to create a table with a div inside each td which is created by clicking buttons... for example
Please select the number of rows in the table
Please select the number of columns in the table..
Result:
So if you clicked on 4 and 4 it would create a table 4 X 4. If you clicked 3 X 1, you would create a table 3 X 1, etc...
Any help would be greatly appreciated!!
Here's a jfiddle of something I'm trying to get working. I'm still looking over all your comments!
http://jsfiddle.net/irocmon/7WD8v/
I know I need to add in the Javascript how to get the element by id.
发布评论
评论(4)
我将使用两种形式,一种用于顶行数字,一种用于第二行数字,其中每个数字都是用户输入的预定义值。
使用 JavaScript 将提交按钮分配给每个表单的每个数字,然后使用 JavaScript 获取结果并执行完成任务所需的代码/脚本。
我建议使用 jquery 来实现这一点。
玩得开心...
I would use 2 forms, 1 for the top row of numbers and one for the second row of numbers, where each number is a predefined value of the user input.
Assign the submit button to each of the numbers using javascript for each form and from there grab the results with javascript and perform the code/script that is required to complete the task in mind.
I would recommend using jquery for this.
Have fun...
你应该能够通过一些非常简单的 if 语句或 switch 来实现这一点
如果你有 2 个变量 rows & , 。 columns
这里的语法非常非常伪,这段代码不会工作,但它可能会让你开始,一旦你有了表,你实际上想用它做什么?
you should be able to achieve this with some pretty simple if statements or a switch
if you have 2 variables rows & columns
the syntax is very very psuedo here, this code wont work but it might get you started, what are you actually wanting to do with the table once you have it?
使用 javascript,有 2 个局部变量:宽度和高度。在每个 DIV 中,都有一个 onclick 函数,将该值分配给正确的变量,然后检查是否已分配两个变量(这样他们可以先单击高度或宽度)。如果两者都是,则在 for 循环中使用这些变量在 javascript 中生成 HTML 代码:
var HTML = '';
for(var i = 0; i < height; i++)
{ HTML += '';
for(var j = 0; j
{ HTML += '...';}
HTML += '';}
document.getElementById('where_you_want_the_table') .innerHTML = HTML;
Using javascript, have 2 local variables: width and height. Within each DIV, have an onclick function that assigns that value to the proper variable, then checks to see if both variables have been assigned (this way they can click on either height or width first). If both are, use these variables within a for loop to generate HTML code within javascript:
var HTML = '<table>';
for(var i = 0; i < height; i++)
{ HTML += '<tr>';
for(var j = 0; j < width; j++)
{ HTML += '<td>...</td>';}
HTML += '</tr>';}
document.getElementById('where_you_want_the_table').innerHTML = HTML;
这是经过测试的,值得注意的是,如果他们继续尝试一遍又一遍地构建表格,它不会处理它会不断添加列和行,但我会让你处理这个问题。 :)
This is tested and work of note it doesn't handle if they keep trying to build the tables over and over it will keep adding cols and rows but I will let you handle that. :)