如何使用Jquery在DIV标记内创建表

How to create a table inside a DIV tag using Jquery?

本文关键字:创建 DIV 何使用 Jquery      更新时间:2023-09-26

我已经使用创建了一个数据表

<script>
 var table = '<table border="0" id="tblTestCases">';
            table+="<tr bgcolor='#e6e6e6'><td colspan='3'>Number of text boxes = " + $searchObject.find('[type=text]').length + "</td></tr>";
            $searchObject.find('[type=text]').each(function () {
                table+="<tr bgcolor='#ccccff'><td>Name of textbox = " + $(this).attr("name") + "</td><td> ID =" + $(this).attr("id") + "</td><td> Test cases : <br /> 1) Check if cursor is displayed/blinking after mouse click <br /> 2)Check if a text can be entered inside</td></tr>";
            });
            table+="<tr bgcolor='#e6e6e6'><td colspan='3'>Number of Submit Buttons = " + $searchObject.find('[type=submit]').length + "</td></tr>";
            $searchObject.find('[type=submit]').each(function () {
                table+="<tr bgcolor='#6666ff'><td>Name of Submit button = " + $(this).attr("name") + "</td><td> ID =" + $(this).attr("id") + "</td><td> Test cases : <br /> 1) Check if the button is clickable or not <br /> 2) Check if the submission result is displayed after mouse click";
            });
</script>

我想把表格放在DIV标签里。但是脚本是在主体内创建表,而不是在我想要的位置。有什么方法可以把同一个表放在DIV内吗?

提前谢谢。PS。表里面有很多条目。我只添加了2个用于演示。

使用以下代码片段作为html:中的容器

<div id="container"></div>

并像一样更新脚本

<script>
var table = '<table border="0" id="tblTestCases">';
        table+="<tr bgcolor='#e6e6e6'><td colspan='3'>Number of text boxes = " + $searchObject.find('[type=text]').length + "</td></tr>";
        $searchObject.find('[type=text]').each(function () {
            table+="<tr bgcolor='#ccccff'><td>Name of textbox = " + $(this).attr("name") + "</td><td> ID =" + $(this).attr("id") + "</td><td> Test cases : <br /> 1) Check if cursor is displayed/blinking after mouse click <br /> 2)Check if a text can be entered inside</td></tr>";
        });
        table+="<tr bgcolor='#e6e6e6'><td colspan='3'>Number of Submit Buttons = " + $searchObject.find('[type=submit]').length + "</td></tr>";
        $searchObject.find('[type=submit]').each(function () {
            table+="<tr bgcolor='#6666ff'><td>Name of Submit button = " + $(this).attr("name") + "</td><td> ID =" + $(this).attr("id") + "</td><td> Test cases : <br /> 1) Check if the button is clickable or not <br /> 2) Check if the submission result is displayed after mouse click";
        });
$("#container").html(table);
</script>
<script>
 var table = '<table border="0" id="tblTestCases">';
            table+="<tr bgcolor='#e6e6e6'><td colspan='3'>Number of text boxes = " + $searchObject.find('[type=text]').length + "</td></tr>";
            $searchObject.find('[type=text]').each(function () {
                table+="<tr bgcolor='#ccccff'><td>Name of textbox = " + $(this).attr("name") + "</td><td> ID =" + $(this).attr("id") + "</td><td> Test cases : <br /> 1) Check if cursor is displayed/blinking after mouse click <br /> 2)Check if a text can be entered inside</td></tr>";
            });
            table+="<tr bgcolor='#e6e6e6'><td colspan='3'>Number of Submit Buttons = " + $searchObject.find('[type=submit]').length + "</td></tr>";
            $searchObject.find('[type=submit]').each(function () {
                table+="<tr bgcolor='#6666ff'><td>Name of Submit button = " + $(this).attr("name") + "</td><td> ID =" + $(this).attr("id") + "</td><td> Test cases : <br /> 1) Check if the button is clickable or not <br /> 2) Check if the submission result is displayed after mouse click";
            });
</script>

现在你有了这个HTML,只需要把它放在div中,例如,这就是HTML:

<div id="testing"></div>

现在做:

$("#testing").html(table);