Onclick事件动态构建div并将其附加到现有的div(jquery或其他任何受欢迎的东西)



我有一个div,格式如下。1.我如何将其添加到现有的div中。我是否创建一个单独的html文件,然后使用jquery添加它?或者我应该把它附加到一个var中,然后再附加到div中。请建议一种方法,1附加并创建div,然后把它附加在另一个div(我知道它的id)中

<div>
  <table>
    <tr><td></td><td></td><td></td>
    </tr>
  </table>
  <table>
    <tr><td></td><td></td>
    </tr>
    <tr><td></td><td></td><td></td><td></td>
    </tr>
  </table>
  <table>
    <tr><td></td>
    </tr>
  </table>
</div>

您可以尝试以下操作(使用jQuery):

HTML:

<div id="content" style="display: none;">
  <div>
    <table>
      <tr>text here and then some input tag
      </tr>
    </table>
    <table>
      <tr>text here and then some input tag
      </tr>
      <tr>text here and then some input tag
      </tr>
    </table>
    <table>
      <tr>text here and then some input tag
      </tr>
    </table>
  </div>
</div>
<div id="receiver1" style="background: #FF0000;"></div>
<div id="receiver2" style="background: #00FF00;"></div>
<div id="receiver3" style="background: #0000FF;"></div>

JS:

$(function(){
    var theContent = $( "#content" ).html();
    $( "#receiver1" ).html( theContent );
    $( "#receiver2" ).html( theContent );
    $( "#receiver3" ).html( theContent );
});

您可以在此处查看示例:http://jsfiddle.net/3hm6N/1/

当然,你可以使用格雷格·佩蒂特所说的方法。还有一件事,我没有看到,但你的表格式错误,因为标签里面应该有<td>s或<th>s。

最新更新