javascript - 如何在javascript中的html表格中使用CSS

标签 javascript html css

这是一个示例代码,用于展示我如何在 javascript 中使用 HTML 标记。我也想将 CSS 样式添加到表中,但它在 javascript 代码中不起作用。谁能建议如何将 CSS 样式添加到我在 javascript 中的 html 表中。

<html>
    <body>
        <style type="text/css">
            table.imagetable 
            {
                font-family: verdana,arial,sans-serif;
                font-size:11px;
                color:#333333;
                border-width: 1px;
                border-color: #999999;
                border-collapse: collapse;
            }
            table.imagetable th 
            {
                background:#b5cfd2 url('cell-blue.jpg');
                border-width: 1px;
                padding: 8px;
                border-style: solid;
                border-color: #999999;
            }
            table.imagetable td 
            {
                background:#dcddc0 url('cell-grey.jpg');
                border-width: 1px;
                padding: 8px;
                border-style: solid;
                border-color: #999999;
            }
        </style>
<script language="JavaScript" >
            function getSubmit()
            {

                var strHtml ="";
                strHtml += "<table class = 'imagetable' >";
                strHtml += "<tr>";
                strHtml += "<th>S No</th>";
                strHtml += "<th>Roll Number</th>";
                strHtml += "<th>Name</th>";
                strHtml += "<th>Maths</th>";
                strHtml += "<th>Physics</th>";
                strHtml += "<th>Chemistry</th>";
                strHtml += "<th>Total</th>";
                strHtml += "<th>Percentage</th>";
                strHtml += "<th>Division</th>"
                strHtml += "</tr>"
                strHtml += "</table>";
                document.write(strHtml);
            }
        </script>
        <div align="center" style="border-bottom-color:#330000">
            <table border="1" bordercolor="#000000" class= "imagetable">
                <th>S No</th>
                <tr align="center">
                    <td><input name="button" type="button" onClick="getSubmit()" value="submit"/></td>
                </tr>
            </table>
        </div>
    </body>
</html>

最佳答案

当你这样做时:

document.write(strHtml);

它会清除页面中的所有 css 和其他 html 标记,这就是为什么您的代码中没有任何内容。 你可以这样做:

var mydiv = document.createElement("div");
document.body.appendChild(mydiv);
mydiv.innerHTML = strHtml;

关于javascript - 如何在javascript中的html表格中使用CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20892782/

相关文章:

javascript - 从 Firebase 获取初始集合值

python - 如何使用 Selenium Python 获取 td web-table 中的文本

javascript - 如何在 AMP html 网站中获取 Medium 博客文章?

html - CSS,DIV,位置为 : absolute overlaps <p> text

html - 如何使用 CSS 将文本环绕在图像周围?

html - 适当调整网站大小

javascript - 如何使标题在 wordpress 中保持粘性

javascript - 在 cakephp 中重新渲染元素 View 后,如何重新绑定(bind) javascript 事件

javascript - 剑道分离器 : How to change "collapsible: false" property of splitter to "collapsible: true" on click of button

javascript - 为什么后递增/递减运算符对循环内的变量没有任何影响?