使用Javascript生成的表格样式

时间:2013-11-08 16:10:44

标签: javascript css

我有一些javascript解析csv文件并将内容放到表格上。

我正在使用一个非常通用的函数生成表,该函数通过2D数组并构建一个html字符串。 然后我做

document.getElementById("StringTable").innerHTML = result;

我的桌子生成得很好,适合我需要的东西。

我想应用一些CSS样式只是为了让它看起来更好一点,但我似乎无法让我的css文件对表有任何影响。

我在考虑它,因为表是在加载css后生成的,但我不知道如何解决这个问题。我该怎么办?

HTML:

<body>
    <div id="StrRecTable">


    </div>
</body>

使用Javascript:

function makeTableHTML(array) {
    var result = "<table border=2, width=1000px>";
    result += "<thead><tr><th>Date</th><th>D_1</th><th>D_2</th><th>D_3</th><th>D_4</th><th>D_5</th><th>D_6</th></thead><tbody>";
    // Create header row. Better way to do this?
    //for (var i = 0; i < array.length; i++) {
    for (var i = array.length-1; i > 0; i--) {
        result += "<tr>";
        for (var j = 0; j < array[i].length; j++) {
            result += "<td>"+array[i][j]+"</td>";   
        }   
        result += "</tr>";
    }   
    result += "</tbody></table>";
    document.getElementById("StrRecTable").innerHTML = result;
}

CSS :(这是从某个网站自动生成的。我只想玩它。

body {background-color:#888888;}

.StrRecTable {
margin:0px;padding:0px;
width:100%;
box-shadow: 10px 10px 5px #888888;
border:1px solid #125b07;

-moz-border-radius-bottomleft:25px;
-webkit-border-bottom-left-radius:25px;
border-bottom-left-radius:25px;

-moz-border-radius-bottomright:25px;
-webkit-border-bottom-right-radius:25px;
border-bottom-right-radius:25px;

-moz-border-radius-topright:25px;
-webkit-border-top-right-radius:25px;
border-top-right-radius:25px;

-moz-border-radius-topleft:25px;
-webkit-border-top-left-radius:25px;
border-top-left-radius:25px;
}.StrRecTable table{
width:100%;
height:100%;
margin:0px;padding:0px;
}.StrRecTable tr:last-child td:last-child {
-moz-border-radius-bottomright:25px;
-webkit-border-bottom-right-radius:25px;
border-bottom-right-radius:25px;
}
.StrRecTable table tr:first-child td:first-child {
-moz-border-radius-topleft:25px;
-webkit-border-top-left-radius:25px;
border-top-left-radius:25px;
}
.StrRecTable table tr:first-child td:last-child {
-moz-border-radius-topright:25px;
-webkit-border-top-right-radius:25px;
border-top-right-radius:25px;
}.StrRecTable tr:last-child td:first-child{
-moz-border-radius-bottomleft:25px;
-webkit-border-bottom-left-radius:25px;
border-bottom-left-radius:25px;
}.StrRecTable tr:hover td{

}
.StrRecTable tr:nth-child(odd){ background-color:#7f7f7f; }
.StrRecTable tr:nth-child(even)    { background-color:#333333; }.StrRecTable td{
vertical-align:middle;


border:1px solid #125b07;
border-width:0px 1px 1px 0px;
text-align:left;
padding:5px;
font-size:10px;
font-family:Arial;
font-weight:normal;
color:#ffffff;
}.StrRecTable tr:last-child td{
border-width:0px 1px 0px 0px;
}.StrRecTable tr td:last-child{
border-width:0px 0px 1px 0px;
}.StrRecTable tr:last-child td:last-child{
border-width:0px 0px 0px 0px;
}
.StrRecTable tr:first-child td{
    background:-o-linear-gradient(bottom, #5fbf00 5%, #5fbf00 100%);    background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #5fbf00), color-stop(1, #5fbf00) );
background:-moz-linear-gradient( center top, #5fbf00 5%, #5fbf00 100% );
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr="#5fbf00", endColorstr="#5fbf00");  background: -o-linear-gradient(top,#5fbf00,5fbf00);

background-color:#5fbf00;
border:0px solid #125b07;
text-align:center;
border-width:0px 0px 1px 1px;
font-size:12px;
font-family:Arial;
font-weight:bold;
color:#ffffff;
}
.StrRecTable tr:first-child:hover td{
background:-o-linear-gradient(bottom, #5fbf00 5%, #5fbf00 100%);    background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #5fbf00), color-stop(1, #5fbf00) );
background:-moz-linear-gradient( center top, #5fbf00 5%, #5fbf00 100% );
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr="#5fbf00", endColorstr="#5fbf00");  background: -o-linear-gradient(top,#5fbf00,5fbf00);

background-color:#5fbf00;
}
.StrRecTable tr:first-child td:first-child{
border-width:0px 0px 1px 0px;
}
.StrRecTable tr:first-child td:last-child{
border-width:0px 0px 1px 1px;
}

编辑添加HTML / CSS

2 个答案:

答案 0 :(得分:2)

CSS使用 .StrRecTable 的类,但您的JavaScript通过id引用它

document.getElementById("StrRecTable").innerHTML = result;

将一个类添加到div中,或者引用CSS中的ID:

<body>
    <div id="StrRecTable" class="StrRecTable">


    </div>
</body>

以下是一个JSFiddle,它更新了CSS以引用持有人DIV的ID。

答案 1 :(得分:1)

  • 您的HTML包含一个带有id StrRecTable的div。
  • 您的CSS定位的是一个带有class StrRecTable

    的元素
    1. .someText将定位具有'someText'class的元素

    2. #someText将使用{someText'的id定位元素

Simples!