添加 iframe 内容的脚本在 Chrome 中没有用户代理样式



>chrome 版本:58

通常,表单将具有用户代理样式margin-bottom: 1em;

<!DOCTYPE html>
<html>
  <head>
    <title>test</title>
    <meta charset="utf-8" />
  </head>
  <body>
    <form action="#">
      First name:<br><input type="text" name="firstname" value="Mickey"><br>
      Last name:<br><input type="text" name="lastname" value="Mouse"><br><br>
      <input type="submit" value="Submit"></form>
      <div>hello world</div>
  </body>
</html>

具有用户代理样式

但是当我通过脚本向 iframe 添加内容时,表单没有样式margin-bottom: 1em;

    <!DOCTYPE html>
    <html>
        <head>
            <title>clack test</title>
            <meta charset="utf-8" />
        </head>
        <body>
            <script>
                var iframe = document.createElement("iframe");
                iframe.id = 'iframe';
                iframe.width = '100%';
                iframe.height = '886';
                iframe.setAttribute("scrolling", "no");
                iframe.setAttribute("frameborder", "0");
                document.body.appendChild(iframe);
                // find the iframe's document and write some content
                var idocument = iframe.contentDocument;
                idocument.open();
                idocument.write("<!DOCTYPE html>");
                idocument.write("<html>");
                idocument.write("<head></head>");
                idocument.write('<body><form action="/demo/demo_form.asp">First name:<br><input type="text" name="firstname" value="Mickey"><br>Last name:<br><input type="text" name="lastname" value="Mouse"><br><br><input type="submit" value="Submit"></form><div>hello world</div></body>');
                idocument.write("</html>");
                idocument.close();
            </script>
        </body>
    </html>

没有用户代理样式

我想知道如何让用户代理样式在 iframe 中工作。谢谢

script = document.getElementById("js");
iframe = document.createElement("iframe");
document.body.appendChild(iframe);
iframe.appendChild(script);
<script id="js">
  // the script you want to inject
  alert("Injected");
  console.log("Injected");
</script>

尝试使用这样的东西

.HTML:

<script id="js">
 // the script you want to inject 
</script>

.JS:

script = document.getElementById("js");
iframe.appendChild(script);

我在LiveWeave做了一个编织。

最新更新