簡體   English   中英

如何使用jQuery將CSS添加到iframe中?

[英]How to add css using jquery into iframe?

我正在嘗試使用jquery將CSS(width:100%)添加到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);
});

代替

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

我寫這個

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

您可以使用Javascript在元素上應用width 。請查看以下代碼段。 謝謝

var iframe = document.createElement('iframe').style.width = "100%";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM