2016-04-27 66 views
0

我已經經歷了很多與此問題相關的主題,但無法獲得所需的輸出。如何使用jQuery處理iframe對象

我裏面和HTML調用一個IFRAME這樣的:

<iframe class="full-screen-preview__frame" id="nitseditpreview" src="himu/index.php" name="preview-frame" frameborder="0" noresize="noresize" data-view="fullScreenPreview"> 

假設在這個iframe中我有一個類名像這樣的H2標籤:

<body> 
    <section id="about-us"> 
    <div class="container"> 
     <div class="text-center"> 
     <div class="col-sm-8"> 
      <h2 class="maincontent"> 
    Why with Us 
    </h2> 
     </div> 
     </div> 
    </div> 
    </section> 
</body> 

所看到的檢查在瀏覽器中的元素

通過使用jquery我想操縱這個讓我們說例如我想在這個邊框。我已經嘗試了很多事情,但我想如果有人修復了這個bug這件事情會的工作,我的jQuery看起來是這樣的:

$(document).load(function() { 
$('#nitseditpreview').load(function() { //The function below executes once the iframe has finished loading 
    $this = $(this); 
    $('#nitsmenu', this.contents()).css('border', 'solid 1px #777'); 
}); 

});

不知道我在做什麼錯誤,即使我也遵循相同的原產地政策。

+0

是內部或外部的伊夫蘭運行JS? – Richard

+0

iframe之外 –

回答

1

如果框架和框架文檔都在同一個域中,則不需要任何沙箱屬性或CORS環箍跳躍。但也有其他一些錯誤的位置:

  • $(document).load(...)應該$(document).ready(...)
  • 定義$this = $(this),但隨後在下一行嘗試使用(因爲它已經被你的腳本運行時加載)裸this
  • 你試圖匹配#nitsmenu不會出現在框架文件

以下似乎工作中存在的,雖然我很擔心有可能仍然是在比賽條件是iframe的​​:

$(document).ready(function() { 
    $('#nitseditpreview').load(function() { 
     $(this).contents().find('.container').css('border', 'solid 1px #777'); 
    }); 
}); 

http://plnkr.co/edit/tCEHdU0ckg5q4w4tPVFU

+0

嘿,感謝您的更新,但是這並沒有幫助我..( –

+0

opps ..對不起,我忘了刪除'sandbox =「allow-scripts」'。現在它的工作完美。噸..:) –