簡體   English   中英

如何從 iframe 內容中的 div 中刪除 class

[英]How to remove class from div inside iframe contents

我有這個結構,我想從div中刪除test2 class

iframe1 -> iframe2 -> <div class="test1 test2">.... </div>

我可以訪問 iframe2,但我無法在第二個 iframe 中找到上面的 div。

這是我試過的

document.querySelectorAll('iframe').forEach(item => {
            var y = item.contentDocument.body.getElementsByTagName("iframe");
            setTimeout(() => {
                y[0].style.width = '100vw'; 
                $(y[0]).contents().find(".test2").removeClass('.test2')
            }, [500])
        })

y[0].style.width = '100vw';

這是工作。 但我無法通過test2找到 div class

console.log(y[0]) 是預期的 iframe。

在此處輸入圖像描述

y[0].contentDocument是 null

如果有人可以幫助我。 提前致謝

同樣在一些答案中,我發現如果 iframe 是跨源的,我們就不能更改它的內容。 但在這里我有相同的起源。

我只想要一些 css 更改,我需要刪除 test2 class 或覆蓋它。

假設如下所示的模擬結構,都具有相同的來源:

index.html

<body>
  <iframe id="iframe1" src="index2.html"></iframe>
</body>

index2.html

<body>
  <iframe id="iframe2" src="index3.html"></iframe>
</body>

index3.html

<body>
  <div class="test2">Foo</div>
</body>

您將需要以下令人困惑的代碼。

var f1 = $("#iframe1");
var f2 = $("#iframe2", f1.contents());
var target = $(".test2", f2.contents());
target.removeClass("test2");

這是基於以下內容: Selecting an element in iframe with jQuery

暫無
暫無

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

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