如何编辑/删除 iFrame 内的内容?

问题描述 投票:0回答:1

我有一个关于编辑/删除 iFrame 中的内容的简单问题。我尝试使用以下方法直接编辑内容:

const doc = Frame.contentDocument || Frame.contentWindow.document;

$(doc).find(".el").remove();

但收到 CORS 错误。那么,问题是“如何编辑/删除 iFrame 内的元素”?

javascript jquery iframe same-origin-policy
1个回答
0
投票
<iframe id="myFrame" src="example.html" width="400" height="300"></iframe>

<script>
    // Access the iframe
    var iframe = document.getElementById('myFrame');
    var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;

    // Access and manipulate elements inside the iframe
    var paragraph = iframeDoc.getElementById('paragraphId');
    paragraph.textContent = 'New content'; // Change text content

    var removeElement = iframeDoc.getElementById('elementToRemove');
    removeElement.parentNode.removeChild(removeElement); // Remove an element
</script>
© www.soinside.com 2019 - 2024. All rights reserved.