内容摘要

  • 父级获取iframe中的元素
  • 父级触发iframe中的函数
  • iframe触发父级元素绑定的事件
  • iframe触发父级函数方法
  • iframe触发父级元素的值

parent.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>parent</title>
<style type="text/css" media="screen">
* {
margin: 0;
padding: 0;
}
iframe {
border: 0;
}
</style>
</head>
<body>
<p>下面为要调用的iframe标签,也可以通过类名来设置宽高</p>
<iframe src="ifr.html" name="ifr" width="100%" height="200" class="ifr" id="ifr"></iframe>
<!-- iframe触发父页面事件 -->
<p>我是父页面的内容,点击iframe中的按钮,触发父页面中的事件,改变 <span class="change1">#1</span></p>
<p class="change11">子级iframe会改变我的内容</p>
<!-- 父页面调用子iframe的方法 -->
<button class="p-btn1">点我调用子iframe中的方法</button>
<div id="parent-val">我是父页面的内容</div>
</body>
<script src="./jquery.min.js"></script>
<script>
function fun() {
alert("点击子元素中的按钮执行父元素中的函数")
}
$(".change1").on('click', function() {
console.log('parent')
$(this).html("#1#1")
})
$(".p-btn1").on('click', function() {
// 调用子iframe中的方法
parent.frames["ifr"].window.childMethods1();
$(".ifr")[0].contentWindow.childMethods1();
document.getElementById("ifr").contentWindow.childMethods1();
// 查找iframe中的元素
console.log($(".ifr").contents().find(".title").html())
// 原生方法候去iframe元素的值
let doc=document.getElementById("ifr").contentWindow.document;
let tit=doc.getElementById("ifr-val").innerHTML;
console.log(tit) })
</script>
</html>

ifr.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>iframe1</title>
</head>
<body>
<div id="ifr-val">我是ifr的内容</div>
<p class="title">我是iframe1</p>
<button type="button" class="ifr1-btn">点我改变父页面内容 #1->#1#1</button>
</body>
<script src="jquery.min.js"></script>
<script>
function childMethods1() {
console.log('我是子iframe中的方法!')
}
$(".ifr1-btn").on('click', function() {
console.log("iframe1")
// 点击触发父页面的事件
parent.$(window.parent.document).find('.change1').click()
// 点击直接改变父页面中的dom
parent.$(window.parent.document).find('.change11').html("gggg")
// 点击执行父页面中定义的方法
parent.fun()
window.parent.fun()
// 获取父元素中元素的内容
console.log($(".p-btn1", window.parent.document).html())
console.log(window.parent.$(".p-btn1").html())
console.log(window.parent.document.getElementById("parent-val").innerHTML) })
</script>
</html>

总结

原生js

父调子

元素:window.frames[iframe的name属性值];

方法:

  • ① document.getElementById("子页面元素ID").contentWindow.子页面方法名;
  • ② document.getElementsByTagName("子页面元素标签名")[i].contentWindow.子页面方法名;
  • ③ document.getElementsByClassName("子页面元素类名")[i].contentWindow.子页面方法名;

子调父

元素:

  • ① window.parent.document.getElementById("父页面元素ID");
  • ②window.parent.document.getElementsByTagName("父页面元素标签名")[i];
  • ③window.parent.document.getElementsByClassName("父页面元素类名")[i];

方法:window.parent.父页面方法;

原生方法还是使用id比较靠谱

jquery

父调子

元素:$(iframe选择器).contents().find(iframe中元素选择器);

// 查找iframe中的元素
console.log($(".ifr").contents().find(".title").html())

方法:$(iframe选择器)[0].contentWindow.子页面方法名;

// 调用子iframe中的方法
parent.frames["ifr"].window.childMethods1();
$(".ifr")[0].contentWindow.childMethods1();

子调父

元素:

  • ① $(父页面元素选择器, window.parent.document);
  • ② window.parent.$(父页面元素选择器)
// 获取父元素中元素的内容
console.log($(".p-btn1", window.parent.document).html())
console.log(window.parent.$(".p-btn1").html())
parent.$(window.parent.document).find('.change11').html("gggg")

方法:window.parent.父页面方法;

// 点击执行父页面中定义的方法
parent.fun()
window.parent.fun()
// 点击触发父页面的事件
parent.$(window.parent.document).find('.change1').click()

iframe相关操作的更多相关文章

  1. jquery 实践操作:iframe 相关操作

    此篇记录关于HTML 的 iframe 元素 的相关记录 定义:iframe 元素会创建包含另外一个文档的内联框架(即行内框架). 常用的基本 iframe 设置(详细设置属性参考API:http:/ ...

  2. javascript iframe相关操作

    1. 获得iframe的window对象 iframeElement.contentWindow 2. 获得iframe的document对象 存在跨域访问限制. chrome:iframeEleme ...

  3. iframe的操作

    获取iframe的window,获取Iframe的document,获取父页面的window,某个环境是否iframe,动态创建iframe 这是demo.html,这个页用iframe嵌入了ifra ...

  4. 从零自学Hadoop(20):HBase数据模型相关操作上

    阅读目录 序 介绍 命名空间 表 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 序 ...

  5. 从零自学Hadoop(21):HBase数据模型相关操作下

    阅读目录 序 变量 数据模型操作 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 序 ...

  6. 理解CSV文件以及ABAP中的相关操作

    在很多ABAP开发中,我们使用CSV文件,有时候,关于CSV文件本身的一些问题使人迷惑.它仅仅是一种被逗号分割的文本文档吗? 让我们先来看看接下来可能要处理的几个相关组件的词汇的语义. Separat ...

  7. Liunx下的有关于tomcat的相关操作 && Liunx 常用指令

    先记录以下liunx下的有关于tomcat的相关操作 查看tomcat进程: ps-ef|grep java (回车) 停止tomcat进程: kill -9 PID (进程号如77447) (回车) ...

  8. pip的相关操作

    >Python中的pip是什么?能够做些什么? pip是Python中的一个进行包管理的东西,能够下载包.安装包.卸载包......一些列操作 >怎么查看pip的相关信息 在控制台输入: ...

  9. python操作mysql数据库的相关操作实例

    python操作mysql数据库的相关操作实例 # -*- coding: utf-8 -*- #python operate mysql database import MySQLdb #数据库名称 ...

随机推荐

  1. NuGet学习笔记(2)——vs2015搭建本地NuGet服务器

    搭建本地服务器特别简单,新建一个web空项目,按照下图所示搜索安装即可,之后设置hosts 将www.mynuget.com执向本机 运行里面输入c:\windows\system32\drivers ...

  2. laravel 多个where的连接使用

    在查询的时候需要用到多个where条件来查询 1.直接多个where连接 ->where()->where() 2.把查询条件 放到where数组$where中 然后 ->where ...

  3. top命令 Linux查看CPU和内存使用情况,cpu监控之一

    一.top命令 top命令是一个功能十分强大的监控系统的工具,对于系统管理员而言尤其重要.但是,它的缺点是会消耗很多系统资源. 在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分 ...

  4. c语言-单链表(二)

    继续复习链表知识点,本章包含单链表的增加,删除,判断是否为空,和链表长度,以及链表的排序 几个知识点 1.链表的判断是否为空 //1.判断链表是否为空 bool isempty_list(PNODE ...

  5. python的ftp上传和下载

    # -*- coding: utf- -*- import os import ftplib USER_NAME = "" PASSWORD = "" SERV ...

  6. Celery-4.1 用户指南: Configuration and defaults (配置和默认值)

    这篇文档描述了可用的配置选项. 如果你使用默认的加载器,你必须创建 celeryconfig.py 模块并且保证它在python路径中. 配置文件示例 以下是配置示例,你可以从这个开始.它包括运行一个 ...

  7. DDD学习笔录——简介DDD的战术模式、问题空间和解空间

    DDD的战术模式 DDD的战术模式(也称为模型构造块)是一个帮助创建 用于复杂有界上下文的有效模型的 模式集合. 也就是我们常说的设计模式. 问题空间 问题空间将问题域提炼成更多可管理的子域,是真对于 ...

  8. hibernateTemplate方法使用

  9. Reporting services

    “数据库引擎服务”可以承载报表服务器数据库.Reporting Services 需要SQL Server 2008 数据库引擎的本地或远程实例来承载报表服务器数据库.如果同时安装数据库引擎实例和 R ...

  10. PHP处理密码的几种方式

    在 PHP中,经常会对用户身份进行认证.本文意在讨论对密码的处理,也就是对密码的加密处理. 1.MD5 相信很多PHP开发者在最先接触PHP的时候,处理密码的首选加密函数可能就是MD5了,我当时就是这 ...