0

我面临创建我的 firstSection(div)的克隆并将其粘贴到 secondSection 的问题。

<script type="text/javascript">
   $(document).ready(function(){
        $("#firstSection").clone().prependTo("#secondSection");
   });
</script>

<table width="100%" border="1">
<div id="firstSection">
    <tr>
        <td>1</td>
        <td>1</td>
        <td>1</td>
    </tr>
    <tr>
        <td>2</td>
        <td>2</td>
        <td>2</td>
    </tr>
    <tr>
        <td>3</td>
        <td>3</td>
        <td>3</td>
    </tr>
</div>

<div id="secondSection">
    <tr>
        <td>9</td>
        <td>9</td>
        <td>9</td>
    </tr>
</div>
</table>
4

1 回答 1

1

首先,您不能div在 a 中包含元素table(除非它们包含在 a tdorth中,因此这可能是浏览器更正无效标记的结果,但是如果您使用tbody而不是div,它应该可以工作:

<table width="100%" border="1">

    <tbody id="firstSection">
        <tr>
            <td>1</td>
            <td>1</td>
            <td>1</td>
        </tr>
        <tr>
            <td>2</td>
            <td>2</td>
            <td>2</td>
        </tr>
        <tr>
            <td>3</td>
            <td>3</td>
            <td>3</td>
        </tr>
    </tbody>

    <tbody id="secondSection">
        <tr>
            <td>9</td>
            <td>9</td>
            <td>9</td>
        </tr>
    </tbody>

</table>

假设您只想复制tbody/#firstSection元素的内容,而不是创建多重嵌套元素:

$('#firstSection').clone().contents().prependTo('#secondSection');

JS 小提琴演示

于 2012-11-19T13:33:55.563 回答