怎么在iframe页面操作子页面里的对象
我想在iframe的父页面去操作子页面里的内容,但是现在父页面一直获取不到子页面的对象。
这是代码:
这个是子页面:
结果打印出来以后是undefind;
怎么样才能获取到子页面的对象呢?
这是代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>iframe</title>
<style>
*{
margin: 0;
padding:0;
}
.testBox{
width: 100%;
background:gray;
}
.iframeTest{
background:papayawhip;
}
</style>
</head>
<body>
<div class="testBox">
<iframe src="iframe.html" style="height:100%" frameborder="0" id="iframeTest" scrolling="no"></iframe>
</div>
</body>
</html>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script>
var str = $("#iframeTest").contents().find("#test");
console.log(str.html());
</script>
这个是子页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>iframe1</title>
</head>
<body>
<p id="test">哈哈,这是一个测试</p>
</body>
</html>
结果打印出来以后是undefind;
怎么样才能获取到子页面的对象呢?
...全文
相关推荐
iframe子父页面调用js函数示例.docx
JS 中 iframe 之间传值及子页面与父页面应用5. javascript 猎取 iframe 里页面中元素值的方法6. JS 实现 iframe 跨页面调用函数的方法7.
js 有框架页面跳转(target)三种情况下的应用
ContentList当前页的iframe名字在有多个iframe的页面中,我们可能需要针对某个特定的iframe执行跳转操作。
chat:跨iframe消息传递和angularjs带来一些乐趣
跨iframe消息传递通常通过Window对象的`postMessage`方法实现。
iframe父页面与子页面通信及相互调用方法
**通信方式** - **DOM操作**:如果父页面和子页面同源,可以使用JavaScript的`contentWindow`属性或`contentDocument`属性来访问子页面的`window`对象和
在iframe中调用js父页面和子页面方法
获取iframe中的window对象: 首先,我们需要通过`document.getElementById`获取到`iframe`的`DOM`对象,然后通过`contentWindow`属性获取到子页面的


