Change HTML of an iFrame with jQuery?

2019-01-06 16:59发布

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

Thanks for the help.

标签: jquery iframe
4条回答
爷、活的狠高调
2楼-- · 2019-01-06 17:26

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');
查看更多
Bombasti
3楼-- · 2019-01-06 17:28

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

$("#frameDemo").contents().find("div").html("new HTML content goes here");
查看更多
我想做一个坏孩纸
4楼-- · 2019-01-06 17:35

You'll have to parse the iframe content.

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

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

查看更多
手持菜刀,她持情操
5楼-- · 2019-01-06 17:39

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>
查看更多
登录 后发表回答