簡體   English   中英

從第二個 iframe 獲取內容

[英]Get contents from second iframe

目前,我可以使用$('iframe').contents()在不是我的 web 頁面上檢索第一個 iframe 的內容

從這里我可以通過$('iframe').contents().find('body')獲取正文標簽。 結果是預期的

但是,我試圖獲取第二個 iframe 的主體標簽。 通過執行 `$('iframe').eq(1) 我可以獲得第二個 iframe,但 $('iframe').eq(1).contents() 什么也得不到。

如何獲取第二個 iframe 的內容?

很多小提琴之類的可能不是一個好的測試場所。 CORS 存在並發症。 這是一個有效的示例:

https://jsfiddle.net/Twisty/037yueqj/9/

HTML

<iframe id="frame-1"></iframe>
<iframe id="frame-2"></iframe>

JavaScript

$(function() {
  var f1 = $("#frame-1"),
    f2 = $("#frame-2"),
    frames = $("iframe");
  f1.contents().find("body").html("<strong>Frame 1</strong>");
  f2.contents().find("body").html("<strong>Frame 2</strong>");
  console.log(frames.eq(0).contents().find("body").text());
  console.log(frames.eq(1).contents().find("body").text());
});

如果你給他們唯一的 ID 或類,你可以調用正確的選擇器。 您可以調用更廣泛的選擇器並使用.eq()或使用.each() () 進行迭代。

必須在 chromium 中使用 --disable-web-security 從另一個 url 訪問 iframe 中的內容

暫無
暫無

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

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