gpt4 book ai didi

javascript - 读取 iframe 内容 html

转载 作者:行者123 更新时间:2023-11-30 20:16:56 25 4
gpt4 key购买 nike

在我的页面中,我有一个 iframe,iframe 的源是一个 url,它有另一个服务器的页面。

当页面加载时,它会获取输出并将其显示在 iframe 中。

现在我想使用下面的代码在 iframe 完全加载后复制它的内容 --

<html>
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script type="text/javascript">
function getContent()
{
var frame = window.frames["iView"].document;
alert(frame.getElementById('CMS').innerHTML);
}
</script>
</head>
<body>
<iframe src="http://www.example.com/abc.jsp" name="iView" style="width:600px;height:800px;border:dotted 1px red" frameborder="0" onload="getContent();"></iframe>
</body>
</html>

但它不起作用,我也尝试过使用 jquery 的多种方式,但在 jquery 中它在加载 iframe 内容之前运行 jquery,这肯定不起作用。请提出建议。

最佳答案

浏览器根据您的代码顺序从上到下加载页面内容。

使用 jquery ready 函数使函数在加载文档后可用。在这种情况下,您的代码应如下所示:

<html>
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script type="text/javascript">

$(document).ready(function(){
function getContent()
{
var frame = window.frames["iView"].document;
alert(frame.getElementById('CMS').innerHTML);
}

getContent();
});
</script>
</head>
<body>
<iframe src="http://www.example.com/abc.jsp" name="iView" style="width:600px;height:800px;border:dotted 1px red" frameborder="0"></iframe>
</body>
</html>

关于javascript - 读取 iframe 内容 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51838060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com