使用 jQuery 更改 iFrame 的 HTML?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5924936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:01:26  来源:igfitidea点击:

Change HTML of an iFrame with jQuery?

jqueryiframe

提问by Stephen Ou

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

有没有办法使用 jQuery 操作来自同一域的 iframe 的 HTML?

Thanks for the help.

谢谢您的帮助。

回答by c0mm0n

You'll have to parse the iframe content.

您必须解析 iframe 内容。

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

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

更多信息:http: //api.jquery.com/contents/

回答by Town

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

您可以使用contents()来操作iframe.

$("#frameDemo").contents().find("div").html("new HTML content goes here");

回答by Shahin

Here is an example from the jQuery documentation:

这是jQuery 文档中的一个示例:

<!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>

回答by shasi kanth

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

如果要更改<body>iframe 标签内的内容,可以使用以下代码:

$("#iframe_id").contents().find("body").html('my_new_content');