有需求联系

Information

- 信息浏览 -

layui(layer相关 iframe操作)

2023-12-17 295

获取 iframe 页中的元素

layer.getChildFrame(selector, index);

  • 参数 selector : iframe 子页面的选择器或元素对象

  • 参数 index : 打开弹层时返回的唯一索引

该方法用于在父页面获取 iframe 子页面中的元素

layer.open({

  type: 2, // iframe 层

  content: '/layer/test/iframe.html',

  success: function(layero, index){

    // 获取 iframe 中 body 元素的 jQuery 对象

    var body = layer.getChildFrame('body', index);

    // 给 iframe 页中的某个输入框赋值

    body.find('input').val('Hello layer.');

  }

});

在 iframe 页中获取弹层索引

layer.getFrameIndex(window.name);

  • 参数 window.name : 当前 iframe 窗口的 name 属性值

该方法用于在 iframe 页面内部获取当前 iframe 弹层的索引,以便关闭自身。

var index = parent.layer.getFrameIndex(window.name); // 获取当前 iframe 层的索引

parent.layer.close(index); // 关闭当前 iframe 弹层

设置 iframe 层高度自适应

layer.iframeAuto(index);

  • 参数 index : 打开弹层时返回的唯一索引

该方法可让 iframe 高度跟随内容自适应

layer.open({

  type: 2, // iframe 层

  content: '/layer/test/iframe.html',

  area: '600px', // 弹层初始宽度

  success: function(layero, index, that){

    layer.iframeAuto(index); // 让 iframe 高度自适应

    that.offset(); // 重新自适应弹层坐标

  }

});


Copyright © 2024 镇江小蚂蚁信息科技有限公司 All Rights Reserved.