移动端开发框架mui之页面之间参数传递(多种方式)

方式一:mui---通过mui.openWindow传递参数

openWindow_send_page.html(发送页面)

<!doctype html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
        <link href="css/mui.min.css" rel="stylesheet" />
    </head>

    <body>
        <button type="button" class="mui-btn mui-btn-green">用mui-openWindow向recieve_page页面传值</button>
        <script src="js/mui.min.js"></script>
        <script type="text/javascript">
            mui.init();
            //openWindow方式
            document.getElementsByTagName('button')[0].addEventListener('tap', function() {
                mui.openWindow({
                    url: 'openWindow_recieve_page.html',
                    id: 'openWindow_recieve_page',
                    extras: {
                        name: 'curry'
                    }
                });
            });
        </script>
    </body>

</html>

openWindow_recieve_page.html(接收页面) 

<!doctype html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
        <link href="css/mui.min.css" rel="stylesheet" />
    </head>

    <body>
        <span id="span1">
            span1
        </span>
        <script src="js/mui.min.js"></script>
        <script type="text/javascript">
            mui.init();
            mui.plusReady(function() {
                var self = plus.webview.currentWebview();
                //或通过webview的id找到
                //var self=plus.webview.getWebviewById('openWindow_recieve_page');
                document.getElementById("span1").innerText = 'hi,' + self.name;
            })
        </script>
    </body>

</html>

方式二:mui---通过plus.webview.create创建webview并打开新页面并传参到新页面

create_webview_home.html(主页)

<!doctype html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
        <link href="css/mui.min.css" rel="stylesheet" />
    </head>

    <body>
        <button id="button1" class="mui-btn mui-badge-purple">打开子页</button>
        <script src="js/mui.min.js"></script>
        <script type="text/javascript">
            mui.init();
            document.getElementById("button1").addEventListener('tap', function() {
                mui.plusReady(function() {
                    var nwating = plus.nativeUI.showWaiting(); //显示原生等待框
                    var webview_sub = plus.webview.create(
                        'create_webview_sub.html',
                        'create_webview_sub', {}, {
                            name: 'davis'
                        }); //后台创建webview并打开页面
                    webview_sub.addEventListener('loaded', function() {
                        nwating.close(); //关闭等待框
                        webview_sub.show('slide-in-right', 150); //把新的webview窗口显示出来
                    }, false);
                });
            })
        </script>
    </body>

</html>

create_webview_sub.html(新页面)

<!doctype html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
        <link href="css/mui.min.css" rel="stylesheet" />
    </head>

    <body>
        <div id="div1"></div>
        <script src="js/mui.min.js"></script>
        <script type="text/javascript">
            mui.init();
            mui.plusReady(function  () {
                document.getElementById("div1").innerText='hi,'+plus.webview.currentWebview().name;
            })
        </script>
    </body>

</html>

方式三:mui---mui.fire触发自定义事件传事件对象中的参数

fire_event_send_page.html(发送页面)

<!doctype html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
        <link href="css/mui.min.css" rel="stylesheet" />
    </head>

    <body>
        <button id="btn_send" type="button" class="mui-btn mui-badge-danger">使用mui.fire触发自定义事件传递参数</button>
        <script src="js/mui.min.js"></script>
        <script type="text/javascript">
            mui.init();
            var ws=null;
            mui.plusReady(function  () {
                ws=plus.webview.create('fire_event_recieve_page.html','fire_event_recieve_page');
            });
            document.getElementById("btn_send").addEventListener('tap',function() {
                //'my_event'是自定义事件名称,name是事件参数 ,监听事件中回调用函数可用event.detail.name获取
                mui.fire(ws,'my_event',{name:'zyz'});
                ws.show();
            });
        </script>
    </body>

</html>

fire_event_recieve_page.html(接收页面)

<!doctype html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
        <link href="css/mui.min.css" rel="stylesheet" />
    </head>

    <body>
        <div id="div1"></div>
        <script src="js/mui.min.js"></script>
        <script type="text/javascript">
            mui.init();
            //监听事件my_event
            window.addEventListener('my_event',function(event) {
                //event是事件对象,可以得到触发时的事件参数
                document.getElementById("div1").innerText='hi,'+event.detail.name;
            })
        </script>
    </body>

</html>

方式四:mui---webview对象调用evalJS来传递参数

evalJS_send_page.html(发送页面)

<!doctype html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
        <link href="css/mui.min.css" rel="stylesheet" />
    </head>

    <body>
        <button id="btn_send" type="button" class="mui-btn mui-btn-blue">发送页面执行接收页面定义的函数并传参</button>
        <script src="js/mui.min.js"></script>
        <script type="text/javascript">
            mui.init();
            var ws = null;
            mui.plusReady(function() {
                //如果使用html5+api,如plus.barcode,plus.webview,plus.nativeUI等需要放入plusReady事件中,而且要用真机运行
                ws = plus.webview.create('evalJS_recieve_page.html', 'evalJS_recieve_page');
            });
            document.getElementById("btn_send").addEventListener('tap', function() {
                var name = 'mike';
                ws.evalJS('get_para("' + name + '")'); //evalJS执行字符串里的js代码
                ws.show();
            })
        </script>
    </body>

</html>

evalJS_recieve_page.html(接收页面)

<!doctype html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
        <link href="css/mui.min.css" rel="stylesheet" />
    </head>

    <body>
        <div class="mui-content">
            <div id="div1"></div>
        </div>
        <script src="js/mui.min.js"></script>
        <script type="text/javascript">
            mui.init();
//            定义一函数给发送页面调用,获得其传入的参数
            function get_para(para) {
                document.getElementById("div1").innerText='hi,'+para;
            }
        </script>
    </body>

</html>

 

 


原文地址:

https://www.cnblogs.com/beast-king/p/9113831.html

https://www.cnblogs.com/beast-king/p/9118748.html

https://www.cnblogs.com/beast-king/p/9113630.html

https://www.cnblogs.com/beast-king/p/9113383.html

 

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在UI 中,可以通过 `mui.openWindow()` 方法打开一个新页面,并通过 `extras` 参数传递数据。接收数据的方式有两种: 1. 使用 `currentWebview` 对象接收 在新页面中,可以通过 `plus.webview.currentWebview()` 获取当前窗口的 `webview` 对象,该对象包含了在打开该窗口时传递的 `extras` 数据。可以通过访问 `currentWebview` 对象的 `extras` 属性来获取数据。 例如,在打开新页面时传递了一个名为 `name` 的参数: ```javascript mui.openWindow({ url: 'newPage.html', extras: { name: 'John' } }); ``` 在新页面中可以通过以下方式获取该参数: ```javascript var currentWebview = plus.webview.currentWebview(); var name = currentWebview.extras.name; console.log(name); // 输出:John ``` 2. 使用 `options` 参数接收 在新页面中,可以使用 `options` 参数接收传递的数据。在打开新页面时,可以将 `options` 对象传递给 `mui.openWindow()` 方法的第二个参数。在新页面中,可以通过访问 `options` 对象获取数据。 例如,在打开新页面时传递了一个名为 `name` 的参数: ```javascript mui.openWindow('newPage.html', { name: 'John' }); ``` 在新页面中可以通过以下方式获取该参数: ```javascript var options = mui.extend(true, {}, plus.webview.currentWebview().options); var name = options.name; console.log(name); // 输出:John ``` 需要注意的是,如果在新页面中使用了 `mui.init()` 方法初始化了当前页面,`options` 对象会被 MUI 自动转换为 `window.__mui_options` 对象,因此可以通过访问 `window.__mui_options` 对象获取数据。例如: ```javascript mui.init({ keyEventBind: { backbutton: false // 禁用后退按钮 } }); console.log(window.__mui_options.name); // 输出:John ``` 以上就是在 MUI 中通过 `extras` 参数传递数据并在新页面中接收的两种方式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值