如何在iframe中插入表格?

时间:2015-02-28 22:37:23

标签: javascript jquery html iframe html-table

我想在没有iframe的情况下插入这段代码:

我有以下HTMLJQUERY代码,它完美无缺:

$('#info').append('<h3>' + 'Sectores de la comuna de Curicó' + '</h3>' +
      '<table id="sectoresTableID" class="table table-hover table-condensed" style="width:99%">' + 
          '<head>' +
            '<tr>' + 
              '<th>Número</th>' + 
              '<th>Sector</th>' +
             '</tr>' +
          '</head>' +
          '<body>' +
          '</body>' +
      '</table>');

但是现在,我想在Iframe中插入此表,我试图在追加中插入一个表标记,但它不起作用。

1 个答案:

答案 0 :(得分:1)

如果iframe位于与您的域不同的域中,则很可能不可能,除非iframe内容明确授予权限。如果iframe位于同一个域中,您可以这样做。

$(document.getElementById("iframeID").contentDocument.body).append('<h3>' + 'Sectores de la comuna de Curicó' + '</h3>' +
                              '<table id="sectoresTableID" class="table table-hover table-condensed" style="width:99%">' + 
                                  '<head>' +
                                    '<tr>' + 
                                      '<th>Número</th>' + 
                                      '<th>Sector</th>' +
                                     '</tr>' +
                                  '</head>' +
                                  '<body>' +
                                  '</body>' +
                              '</table>');

这里的诀窍是你必须先访问contentDocument才能追加任何内容。