乐闻世界logo
搜索文章和话题

How do I refer to the document object of an < iframe > from the parent page, using jQuery?

1个答案

1

When using jQuery to interact with an event on the method to access the document object of the

Here is a specific example:

Suppose you have a page containing an

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Parent Page</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <iframe id="myIframe" src="iframe-content.html" onload="iframeLoaded()"></iframe> <script> function iframeLoaded() { var iframeDoc = $('#myIframe').contents(); // Now you can use iframeDoc to manipulate the <iframe>'s DOM. // For instance, change the background color of the body inside the <iframe>. iframeDoc.find('body').css('background-color', 'lightblue'); } </script> </body> </html>

In this example, we first ensure the event handler to the function, we retrieve the document object of the . Subsequently, we locate the <body> tag within the selector and modify its background color via the .css() method.

This approach is particularly useful when you need to control the content or styling of the

2024年8月13日 11:06 回复

你的答案