用于填写表格html单元格的Javascript

时间:2014-03-29 05:18:21

标签: javascript html html-table cells

我正在尝试创建一个可以制作+,x,H和不同颜色的表,然后点击它绘制在表格上。任何人都可以帮我吗?我不希望你写完整的代码,但如果你能提供一些建议和/或示例代码,我会非常感激!

<!HTML>
<html>
<head>
<title>JScript</title>
<script language="javascript">

function Design()
{

}

</script>
</head>
<body>

<table border="1px" cellpadding="30px">
<tr><td name="h"></td><td></td><td></td><td></td><td name="h"></tr>
<tr><td></td><td></td><td name="h"></td><td></td><td></td></tr>
<tr><td></td><td></td><td name="h"></td><td></td><td></td></tr>
<tr><td></td><td></td><td name="h"></td><td></td><td></td></tr>
<tr><td name="h"></td><td></td><td></td><td></td><td name="h"></td></tr>
</table>

Pattern Choice: <br>
<input type="radio" name="sign" value="plus"> Plus Sign <br>
<input type="radio" name="sign" value="X"> Letter X <br>
<input type="radio" name="sign" value="H"> Letter H <br><br>

Color Choice: <br>
<select name="color" size="5">
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="yellow">Yellow</option>
<option value="green">Green</option>
<option value="orange">Orange</option>
</select>
</div>

<br><br>
<input type="button" value="Color It" onclick="Design()">
<input type="reset" value="Clear">

</body>
</html>

我不知道你是否能看到,但我<td name="h">我认为这将是至少为H填写每个单元格的方法然后我将使用多个名称如果可能的话,它们会重叠吗?

2 个答案:

答案 0 :(得分:1)

我是使用javascript

完成的
 <html>
<head>

          function Design() {
            var desgn;
            if (document.getElementById('r1').checked) {
                desgn = document.getElementById('r1').value;
            }
            if (document.getElementById('r2').checked) {
                desgn = document.getElementById('r2').value;
            }
            if (document.getElementById('r3').checked) {
                desgn = document.getElementById('r3').value;
            }
            console.log(desgn);
            var e = document.getElementById("list");
            var colr = e.options[e.selectedIndex].value;

            var tabs = document.getElementById("tbl");
            var rows = tabs.rows.length;
            var trs = tabs.getElementsByTagName("tr")[0];
            var tds = trs.cells
            var colms = tds.length

            var table = document.getElementById("tbl");
            for (var i = 0, row; row = table.rows[i]; i++) {
                for (var j = 0, col; col = row.cells[j]; j++) {
                    if (desgn == 'X') {
                        var y = rows - i - 1;
                        if (i == j || j == y) {
                            trs = tabs.getElementsByTagName("tr")[i];
                            trs.cells[j].style.backgroundColor = colr;
                        }

                    }
                    else if (desgn == 'plus') {
                        if (i == 2 || j == 2) {
                            trs = tabs.getElementsByTagName("tr")[i];
                            trs.cells[j].style.backgroundColor = colr;
                        }
                    }
                    else if (desgn == "H") {
                        if (j == 1 || j == 3 || i == 2 && j == 2) {
                            trs = tabs.getElementsByTagName("tr")[i];
                            trs.cells[j].style.backgroundColor = colr;
                        }
                    }
                }
            }
        }
</script>

</head>
<body>

   <table border="1px" cellpadding="30px" id="tbl">
<tr><td name="h"></td><td></td><td></td><td></td><td name="h"></tr>
<tr><td></td><td></td><td name="h"></td><td></td><td></td></tr>
<tr><td></td><td></td><td name="h"></td><td></td><td></td></tr>
<tr><td></td><td></td><td name="h"></td><td></td><td></td></tr>
<tr><td name="h"></td><td></td><td></td><td></td><td name="h"></td></tr>
</table>

Pattern Choice: <br>

<input id='r1' type="radio" name="sign" value="plus"> Plus Sign <br>
<input id='r2' type="radio" name="sign" value="X"> Letter X <br>
<input id='r3' type="radio" name="sign" value="H"> Letter H <br><br>

Color Choice: <br>
<select id='list' name="color" size="5">
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="yellow">Yellow</option>
<option value="green">Green</option>
<option value="orange">Orange</option>
</select>
</div>

<br><br>
<input type="button" value="Color It" onclick="Design()">
<input type="reset" value="Clear" >


</body>
</html>

答案 1 :(得分:0)

我是使用jquery

完成的

              

        function Design() {
            var desgn = $('body input[type=radio]:checked').val();
            var colr = $('body select option:selected').text();
            var MyRows = $('table').find('tr');
            var MyCells = $('table').find('tr').find('td');
            var MyColm = MyCells.length / MyRows.length
            for (var i = 0; i < MyRows.length; i++) {
                for (var j = 0; j < MyColm; j++) {
                    if (desgn == 'X') {
                        var x = MyRows.length;
                        var y = x - i - 1;
                        if (i == j || j == y)
                            $('body table tr:eq(' + i + ') td:eq(' + j + ')').css('background-color', colr);
                    }
                    else if (desgn == 'plus') {
                        if(i==2||j==2)
                            $('body table tr:eq(' + i + ') td:eq(' + j + ')').css('background-color', colr);
                    }
                    else if(desgn=="H") {
                        if (j == 1 || j == 3 ||i==2 && j==2)
                            $('body table tr:eq(' + i + ') td:eq(' + j + ')').css('background-color', colr);
                    }
                }
            }

        }

</script>

</head>
<body>

<table border="1px" cellpadding="30px">
<tr><td name="h"></td><td></td><td></td><td></td><td name="h"></tr>
<tr><td></td><td></td><td name="h"></td><td></td><td></td></tr>
<tr><td></td><td></td><td name="h"></td><td></td><td></td></tr>
<tr><td></td><td></td><td name="h"></td><td></td><td></td></tr>
<tr><td name="h"></td><td></td><td></td><td></td><td name="h"></td></tr>
</table>

Pattern Choice: <br>

<input type="radio" name="sign" value="plus"> Plus Sign <br>
<input type="radio" name="sign" value="X"> Letter X <br>
<input type="radio" name="sign" value="H"> Letter H <br><br>

Color Choice: <br>
<select name="color" size="5">
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="yellow">Yellow</option>
<option value="green">Green</option>
<option value="orange">Orange</option>
</select>
</div>

<br><br>
<input type="button" value="Color It" onclick="Design()">
<input type="reset" value="Clear" onclick="clearall()">


</body>
</html>