javascript - iframe怎么获得子页面中的元素?
阿神
阿神 2017-04-11 09:07:04
[JavaScript讨论组]

如题,父容器<p id='father'></p>中嵌入一个iframe,iframe中有一个子元素<p id='child'></p>,js或者jquery怎么通过#father找到#child? 直接写好像是不行的..
谢谢!

阿神
阿神

闭关修行中......

全部回复(4)
PHPz

当然了, 因为是两个页面, 应该先找到iframe的 document然后再找#child

PHPz

$('#iframe的ID').contents().find('#child')

PHP中文网

<html>

<head>
    <title>1.html</title>
</head>

<body>
    <input type="button" onClick="show()" value="2.html的内容">
    <iframe src="new_file2.html" name="myframe"></iframe>
</body>
<script type="text/javascript">
    function show() {
        var xx = window.frames["myframe"].document.getElementById("bd").innerHTML;
        console.log(xx)
    }
</script>

</html>

<html>

<head>
    <title>2.html</title>
</head>

<body id="bd">
    111 <br />
</body>

</html>

PHP中文网

window.frames 返回一个类数组对象,返回当前窗口的所有子框架元素.

window.frames[0].document.querySelector("#child");
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号