简体   繁体   English

将CSS添加到HTML中表格的动态添加行中

[英]Adding css to the dynamically added rows for a table in the html

I have written a script that adds rows (having data) for a table dynamically. 我编写了一个脚本,可以动态地为表添加行(具有数据)。 This is working fine for me. 这对我来说很好。 some times I come across rows that don't have data BUT I still want to insert a blank row. 有时我遇到没有数据的行,但我仍然想插入空白行。 Currently the width and height of blank row is not same as that for the row having data. 目前的宽度和空白行的高度是不一样的,对于具有数据的行。

I though I can solve this problem by fixing the width and height of the row. 我虽然可以通过固定行的宽度和高度来解决此问题。 I want to do this because I want to have uniformity among the rows that have data in them and blank rows. 之所以要这样做,是因为我希望在其中有数据的行与空白行之间保持一致。

I think I have to use css property in the jquery for these manipulations. 我想我必须在jquery中使用css属性进行这些操作。

I want to know how to add css for the rows that are dynamically added?? 我想知道如何为动态添加的行添加CSS?

I have written a script that creates a row in jquery and adds it to the table status 我编写了一个脚本,该脚本在jquery中创建一行并将其添加到表状态

 <script>
socket.on('stat', function (data) {
        var row = $("<tr><td>"+data+"</td></tr>");  
            $("#status").append(row);                   
    });
   </script>

Now to add css I tried something like this. 现在添加CSS,我尝试了类似的方法。 I created a class called newRow. 我创建了一个名为newRow的类。

<style type ="text/css">

.newRow{

        width: 200px;
        height: 200px;
   }
</style>


 <script>
    socket.on('stat', function (data) {
            var row = $("<tr><td>"+data+"</td></tr>");  
                $("#status").append(row);
                   $("#"+row).addClass('newRow');  // Is this right way of adding a class

        });

row.className = "newRow";  // Is this correct.

</script>

This doesn't seem to work. 这似乎不起作用。

I also tried doing this. 我也尝试这样做。

 <script>
socket.on('stat', function (data) {
        var row = $("<tr><td>"+data+"</td></tr>");  
            $("#status").append(row).css("width" : 200px; "height: :200px);                 
    });
   </script>

this also doesn't work. 这也不起作用。

You CSS has the wrong syntax and you should not create a jQuery object with your <tr><td> wich is only a template. 您的CSS语法错误,并且您不应使用<tr><td>仅创建模板来创建jQuery对象。 This should work : 这应该工作:

    var row = "<tr><td>"+data+"</td></tr>"  
    $("#status").append(row).css({width : '200px', height: '200px'});  //CSS need to be an object for multiple values.

   //OR

    var row = "<tr><td>"+data+"</td></tr>"  
    $('#status tr').removeClass('newRow');
    $("#status").append(row).addClass('newRow');

Not sure why you want to add a blank row (could you use padding to achieve the same effect?). 不确定为什么要添加空白行(可以使用填充来达到相同的效果吗?)。 But sometimes, for some browsers, you need to add a space to an empty element to get it to take the width and height declarations. 但是有时候,对于某些浏览器,您需要在一个空元素上添加一个空格,以使其采用width和height声明。

<td>&nbsp;</td>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM