表 - 合并多个&与jQuery单独使用

时间:2014-03-18 06:29:55

标签: jquery

我在HTML设计页面的下方使用了表格结构,但我想点击按钮 事件和我想在html页面下面的输出定义

<div>
        <button type="button" class="btnmergecell imgClass2" style="float: left"></button>
        <br />
        <table id="tblClaimedbyOthers">
            <thead>
                <tbody>
                <tr>
                    <th class="iconth" width="300px">

                        <span class="col-center">Intake User</span>
                    </th>
                    <td> abc </td>
                </tr>
                <tr>
                <tr>
                    <th class="iconth" width="300px">
                        <span class="col-center">Billing</span>
                    </th>
                    <td> def </td>
                </tr>
                <tr>
                    <th class="iconth" width="300px">
                        <span class="col-center">Pharmacist</span>
                    </th>
                    <td> pqr </td>
                </tr>
                </tbody>
            </thead>         
         </table>
        <table id="tblClaimedbyOthers">
            <thead>
                <tbody>
                    <tr>
                        <th class="iconth" width="300px">
                            <span class="col-center">Intake User</span>
                        </th>
                    </tr>
                    <tr><td> abc </td></tr>
                <tr>
                    <th class="iconth" width="300px">
                        <span class="col-center">Billing</span>
                    </th>
                </tr>
                <tr><td> def </td></tr>
                <tr>
                    <th class="iconth" width="300px">
                        <span class="col-center">Pharmacist</span>
                    </th>
                </tr>
                <tr>
                    <td> pqr </td>
                </tr>
            </tbody>
            </thead>
       </table>
    </div>

Input

Intake User     abc
Billing         def
Pharmacist      pqr

Output

Intake User
Abc
Billing
Def
Pharmacist
pqr


1 个答案:

答案 0 :(得分:2)

如果你想创建一个新的输出表作为新表,你可以尝试这个代码,

$('#btn').click(function () {
    var newTable = '<table id="tb2ClaimedbyOthers" border="1"><thead><tbody>';
    $('#tblClaimedbyOthers tr').each(function () {
        newTable += '<tr><th class="iconth" width="300px"><span class="col-center">' + $(this).find('th span').html() + '</span></th></tr><tr><td>' + $(this).find('th').next().html() + '</td></tr>';
    });
    newTable += ' </tbody></thead> </table>';
    $( "body" ).append(newTable);
});

查看DEMO

如果您想在同一张表中输出您的输出表,则可以尝试使用此代码,

$('#btn').click(function () {
    var newTable = '<table id="tb2ClaimedbyOthers" border="1"><thead><tbody>';
    $('#tblClaimedbyOthers tr').each(function () {
        newTable += '<tr><th class="iconth" width="300px"><span class="col-center">' + $(this).find('th span').html() + '</span></th></tr><tr><td>' + $(this).find('th').next().html() + '</td></tr>';
    });
    newTable += ' </tbody></thead> </table>';
    $( "#tblClaimedbyOthers" ).html(newTable);
});

查看DEMO