首页 > 解决方案 > 如何使用 jquery 将 css 添加到 iframe 中?

问题描述

我正在尝试将CSS(width:100%)使用 jquery 添加到 iframe 中。但不知道我在哪里做错了。我尝试了下面提到的代码来使用 jquery 在 iframe 中添加 CSS。HTML 文件

     <div class="col-md-12">

       <div class="page-content ask-question" style="margin-top: 5px;">

           <div id="preview-container" style="height: 342px;">

           </div>

       </div>
   </div>

jQuery代码

$('.ace_text-input').keyup(function(e) {

delay(function(){
    var html1 = buildSource(html, js, css);
    var iframe = document.createElement('iframe');

    iframe.src = 'about:blank';
    iframe.frameBorder="0";
      iframe.height = 496;
      iframe.css='width:100%;';
    iframe.className = 'preview-iframe';

    $('.preview-iframe').remove();
    $('div#preview-container').append(iframe);

    iframe.contentWindow.document.open('text/html', 'replace');
    iframe.contentWindow.document.write(html1);
    iframe.contentWindow.document.close();
},1000);
});

标签: javascriptjqueryhtmliframe

解决方案


代替

iframe.css='width:100%;';

我写这个

iframe.setAttribute( "Style", "width : 100%;");

推荐阅读