gpt4 book ai didi

javascript - 使用 jquery 访问 iframe 的
转载 作者:行者123 更新时间:2023-11-30 18:19:00 24 4
gpt4 key购买 nike

我看过一些关于访问 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 复制...

最佳答案

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

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

根据 OP 要求更新。

当你有两个元素时,你可以使用 ids 而不是 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);

});

关于javascript - 使用 jquery 访问 iframe 的 <table>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12541608/

24 4 0