版权声明:第一次写文章,有什么需要补充的还望各位大神多多指教。 https://blog.csdn.net/mengxiangxingdong/article/details/84822305
目标 新的iframe操控使用它的iframe
场景
假设Iframe A 中使用了layui.open 打开了Iframe B, 也就是说A使用了b, 此时我们解决的问题就是B如何操控A,当然A操控B更加简单
解决方法
1.layui.open 打开时success 回调 在新打开的iframeB 自定义useIframe 属性,传递当前iframeA的name属性
var index = layer.open({
type : 2,
title : bootTitle,
content : bootUrl,
success : function(layero, index) { // 弹出时 把当前页面的window对象
// layero 打开窗口的元素对象 ,index 窗口的索引
var $iframe = $(layero).find('iframe')[0]; // 获取打开的iframe对象
// 获取当前页面的iframe 的name属性
$iframe.contentWindow.useIframe = window.name;
}
});
2.然后iframeB 中 使用这个自定义属性
//获取父亲的界面 然后 找到父亲的界面的iframe ,然后操作这个iframe
var $doc = $(parent.$("#"+window.useIframe)[0].contentWindow.document);
//正常jquery 即可操作
$doc.find("#"+showIdDom).val(treeId);
扩展
1.这样解决问题是因为新增的iframe 存在一个name属性,这个name 是和window.name 是一样的,且不会改变的
2.layui 打开的iframe id 和name 是一个名称
参考资料 https://blog.csdn.net/fengri5566/article/details/52105685