简体   繁体   English

jQuery中的备用行表

[英]alternate row table in jquery

How to create below mentioned table structure in jquery/javascript: 如何在jquery / javascript中创建以下提到的表结构:

  <table>
    <tr>
        <td>
            one
        </td>
        <td>
            two
        </td>
        <td>
            three
        </td>
    </tr>
    <tr>
        <td>
           single
        </td>

    </tr>
      <tr>
        <td>
            one
        </td>
        <td>
            two
        </td>
        <td>
            three
        </td>
    </tr>
     <tr>
        <td>
           single
        </td>

    </tr>
</table>

ie alternate row is having 3/ 1 cols. 即替代行具有3/1列。

This should suffice: 这样就足够了:

$(document).ready(function(){
    var $myTable = $("<table></table>");
    var $myRow;
    var rowCount = 10; //or whatever you want
    var cellCount = 1;
    var i = 0;
    for (i=0; i<rowCount; i++){
        $myRow = $("<tr></tr>");
        cellCount = (i%2 === 0)? 3 : 1;

        for (var j=0; j<cellCount; j++){
           $myRow.append("<td>a</td>") 
        }

        $myTable.append($myRow);
    }

    $("body").append($myTable); //if you want to add it to your document.
});​

Check out the JSFiddle: http://jsfiddle.net/ZbJ3v/ 查看JSFiddle: http//jsfiddle.net/ZbJ3v/

This, of course doesn't add in your "one" "two", etc (I just have 'a' to denote the structure) but I assumed from your question that you just wanted the structure. 当然,这不会在您的“一个”,“两个”等中添加(我只是用“ a”表示结构),但我从您的问题中假设您只是想要该结构。

Edit for comment below . 编辑下面的评论 The loop block would look something like: 循环块如下所示:

    for (i=0; i<rowCount; i++){
        $myRow = $("<tr></tr>");
        if (i%2 === 0){
           $myRow.append("<td>aa.bb</td>") 
           $myRow.append("<td>aa.cc</td>") 
           $myRow.append("<td>aa.dd</td>") 
        }else{
           $myRow.append("<td>xx</td>") 
        }

        $myTable.append($myRow);
    }

Avaliable on this jsfiddle: http://jsfiddle.net/WPJ5q/ 在此jsfiddle上可用: http : //jsfiddle.net/WPJ5q/

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

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