Change HTML of an iFrame with jQuery?

前端 未结 4 2129
无人及你
无人及你 2020-11-29 06:24

Is there a way to manipulate the HTML of an iframe that is from the same domain using jQuery?

Thanks for the help.

相关标签:
4条回答
  • 2020-11-29 07:03

    You can use contents() to manipulate the contents of the iframe.

    $("#frameDemo").contents().find("div").html("new HTML content goes here");
    
    0 讨论(0)
  • 2020-11-29 07:06

    You'll have to parse the iframe content.

    $("#frameid").contents().find("div").html('My html');
    

    More here : http://api.jquery.com/contents/

    0 讨论(0)
  • 2020-11-29 07:07

    If you want to change the contents inside the <body> tag of the iframe, you can use this code:

    $("#iframe_id").contents().find("body").html('my_new_content');
    
    0 讨论(0)
  • 2020-11-29 07:15

    Here is an example from the jQuery documentation:

    <!DOCTYPE html>
    <html>
    <head>
      <script src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <body>
      <iframe src="http://api.jquery.com/" width="80%" height="600" id='frameDemo'></iframe> 
    <script>$("#frameDemo").contents().find("a").css("background-color","#BADA55");</script>
    
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题