使用jquery <table> </table>访问iframe

时间:2012-09-22 07:10:40

标签: javascript jquery jquery-ui iframe

我看过一些关于访问iframe内部数据的文章,所以我在iframe中加载了一个像这样的表格的页面:<table class="table">...</table>

所以我想要显示iframe是这个表,没有任何标题或其他div或任何东西

我如何用jQuery做到这一点?

更新:

html代码如下所示:我在<div>

中添加了表格
<div id="test">
<br>
<table class="table">
<thead>
<tr>
<th> ...... // and so on

所以,我希望显示的是iframe就是这个div

更新 Adil

$(window).load(function () {
        var filteredContents = $('.test').contents().find('.div_iframe').html();
        $('.test').contents().find('body').html(filteredContents);
});

HTML看起来像这样:

<iframe class="test" width="100%" height="100%" src="/message.html?msjId=268" style="height:100%;width:100%;">
<iframe class="test" width="100%" height="100%" src="/message.html?msjId=260" style="height:100%;width:100%;">

以及现在应该显示的表,它们应该是两个不同的表,但目前是msjId的同一个表,但是从第一个msjId重复...

2 个答案:

答案 0 :(得分:1)

您必须使用jquery加载方法而不是准备好。因为加载确保加载了iFrame html。另一方面,ready确保当前页面中的html元素已准备就绪。

$(window).load(function () {  
      var filteredContents = $('#iframeId').contents().find('.table').html();
      $('#iframeId').contents().find('body').html(filteredContents);
});

根据OP请求进行更新。

当你有两个元素时,你可以使用id而不是class,请阅读 selectors

<iframe id="frame1" width="100%" height="100%" src="/message.html?msjId=268" style="height:100%;width:100%;">
<iframe id="frame2" width="100%" height="100%" src="/message.html?msjId=260" style="height:100%;width:100%;">

 $(window).load(function () {
     var filteredContents1 = $('#frame1').contents().find('.div_iframe').html();
     $('#frame1').contents().find('body').html(filteredContents1);

     var filteredContents2 = $('#frame2').contents().find('.div_iframe').html();
     $('#frame2').contents().find('body').html(filteredContents2);

 });

答案 1 :(得分:1)

尝试以下方法:

$('iframe-id').contents().find('body').html($('.table').html());

但iframe页面必须与您的包含页面在同一个域中提供,否则您将收到权限被拒绝错误。