本文主要是介绍layer.js使用心得-向弹出框传值问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
问题背景:
实现弹出对话框加载页面,所以我选择了独立组件layer.js。基于layer.js组件弹出了一个新的iframe的窗口(假设name为LayerFrame,方便后面称呼),但需要从之前的页面传入参数
注:'#sayHello'元素是在主窗口下的 HomeFram(HomeFrame是主窗口下的子 iframe) 里
代码如下:
$('#sayHello').click(function () {top.layer.open({id: "layer_say_hello",type: 2,title: '打招呼',shadeClose: true,shade: 0.8,area: ['500px', '400px'],content: '/Findfriend/Part_SayHello', //iframe的urlsuccess: function (layero, index) {//传入参数,并赋值给iframe的元素}});})
问题描述:
在success回调函数中使用jquery选取不到弹出的 ifram 的元素
问题分析:
经过下午的头破血流,终于被我给分析出来了,功夫不负有心人啊。废话少说,重点来了。
原因是:success回调函数的上下文并不是 主窗口的上下文, 而是 HomeFrame 的上下文。所以在success回调函数中是可以直接找到 HomeFrame 中的元素。如果要找到 LayerFrame 的元素,就必须先找到 LayerFrame 的Dom对象,然后方可找到 LayerFrame 的 document ,最后再去找 LayerFrame 的元素
正确代码如下:
$('#sayHello').click(function () {top.layer.open({id: "layer_say_hello",type: 2,title: '打招呼',shadeClose: true,shade: 0.8,area: ['500px', '400px'],content: '/Findfriend/Part_SayHello', //iframe的urlsuccess: function (layero, index) {var her_uli_id = $("#headico").attr("data-uli-id"); //headico 是 HomeFrame的元素var her_nickname = $("#nickname").text(); //nickname 是 HomeFrame的元素// layero.find("iframe") 找到iframe的jquery对象// layero.find("iframe")[0] 将jqeruy对象转化为Dom对象// contentWindow 获取当前 iframe 的 内容 window对象(Dom对象)//.send-hello 是 LayerFrame 的元素var jquerySendHelloButton = $(".send-hello", layero.find("iframe")[0].contentWindow.document);jquerySendHelloButton.attr("data-her-uli-id", her_uli_id);jquerySendHelloButton.attr("data-her-nick-name", her_nickname);}});})
这篇关于layer.js使用心得-向弹出框传值问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!