javascript - 如何在 iframe 中插入表格?

标签 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 中,我尝试在 Append 中插入表标记,但它不起作用。

最佳答案

如果 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,然后才能附加任何内容。

关于javascript - 如何在 iframe 中插入表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28787791/

相关文章:

javascript - Firebase-admin 不会写入数据库,不会出现错误

javascript - Avada wordpress 网站的简单 CSS 或 JavaScript 加载屏幕,同时加载一个大型视频文件,该文件在页面加载完成后消失

javascript - 我如何告诉 jQuery 忽略空的服务器响应?

javascript - jQuery 无法在外部 javascript 文件中工作

javascript - 有 id 和 name 的 json 字符串?使用 c 锐利

javascript - 使用 jQuery 脚本呈现 html 片段

javascript - 如何从 json 文件加载多个标记?

javascript - 如何为社交媒体网站 : mobile app if mobile, 和桌面标准 URL 编写链接?

html - 在 CSS 中的 "checkmark:checked"条件之前选择一个 div

python - html h1标签不显示类型