在td元素后附加TD('s)

时间:2014-08-12 08:30:40

标签: javascript jquery html

我目前有以下html:

        <tr>
            <td class="success" rowspan="1">Viability</td>
            <td data-rowh="-Checksum">Viability-Checksum</td>
        </tr>
        <tr>
            <td data-rowh="-Request">Viability-Request</td>
        </tr>

我想要做的是使用数据属性add columns after each td data-rowh。 所以它看起来像这样(testdata):

        <tr>
            <td class="success" rowspan="1">Viability</td>
            <td data-rowh="-Checksum">Viability-Checksum</td>
            <td data-check="P1-Checksum">Test P1</td>
            <td data-check="P2-Checksum">Test P2</td>
            <td data-check="P3-Checksum">Test P3</td>
            <td data-check="P4-Checksum">Test P4</td>
            <td data-check="P5-Checksum">Test P5</td>
        </tr>
        <tr>
            <td data-rowh="-Request">Viability-Request</td>
            <td data-check="P1-Request">Test P1</td>
            <td data-check="P2-Request">Test P2</td>
            <td data-check="P3-Request">Test P3</td>
            <td data-check="P4-Request">Test P4</td>
            <td data-check="P5-Request">Test P5</td>
        </tr>

我认为它可以使用以下功能(显然不是)

        $('[data-rowh]').each(function (rIndex, rValue) {
            $.each(data.items, function (index, value) {
                rValue.after("<td data-check='" + value.name + "-" + rValue.data("rowh") + "'>test " + value.name + "</td>");
            })
        });

当我对rIndexrValue使用after()功能时,我收到错误:the property of the method after is not supported by this object。关于我做错了什么的任何想法?

注意:对于那些感兴趣的里程碑看起来像:

"items" : [  
{  "msId": "000104",  "name": "P1",  "msdescription": "Augustus" }
{  "msId": "000105",  "name": "P2",  "msdescription": "Descr" }
{  "msId": "000106",  "name": "P3",  "msdescription": "Test" }
{  "msId": "000107",  "name": "P4",  "msdescription": "456798" }
{  "msId": "000108",  "name": "P5",  "msdescription": "Autumn" }
]

1 个答案:

答案 0 :(得分:1)

在jQuery对象的.each()循环中,当您引用值(或this),而不是 jQuery对象时,您正在使用实际的DOM节点。为了使用jQuery函数,例如.after(),你需要将它包装在jQuery对象中:

$(rValue).after(...);