avst
avst
  • 发布:2015-09-02 13:35
  • 更新:2015-09-02 18:01
  • 阅读:2362

自定义事件传参问题

分类:MUI

main.html 向show.html 页面通过自定义事件传递参数

main.html

mui.each(data,function(i,n){  
                        var livelist = document.getElementById("livelist");  
                        var li  = document.createElement("li");  
                        var a   = document.createElement("a");  
                        var img = document.createElement("img");  
                        var div = document.createElement("div");  
                        var span= document.createElement("span");  
                        var span = document.createElement("span");  

                        a.setAttribute('href','show.html');  
                        a.setAttribute('id',n.id);  
                        //mui.alert(n.id);  
                        img.setAttribute('class','mui-media-object');  
                        img.setAttribute('src',n.livePic ? n.livePic : 'images/logo.png');  

                        div.setAttribute('class','mui-media-body');  
                        div.setAttribute('style','text-align: left; position:relative;');  
                        span.setAttribute('style',' position:absolute; left:3px;');  
                        span.innerText = n.name;  
                        //span.innerText = n.name;  

                        li.className = 'mui-table-view-cell mui-media mui-col-xs-6';  
                        li.appendChild(a);  
                        a.appendChild(img);  
                        a.appendChild(div);  
                        div.appendChild(span);  
                        livelist.appendChild(li);  

                        //open show.html  
                        var liveid = n.id;  
                        live = document.getElementById(liveid);  
                        live.addEventListener('tap','a',function(e){  

                            mui.fire(show,'liveID',{id:liveid});  
                            mui.openWindow({  
                                id:'show',  
                                url:'show.html'  
                            });  
                        });  
                    });

show.html

mui.plusReady(function() {  

        window.addEventListener('liveID',function(event){  
            var liveid = event.detail.id;  
            mui.alert(liveid);  
        });  
    });  

能不能怎样写,如果不能的话该怎么修改

2015-09-02 13:35 负责人:无 分享
已邀请:
DCloud_UNI_CHB

DCloud_UNI_CHB

可以这么写,只是需要注意,自定义事件传递消息的目标webview必须是已经存在的;
另外,代码可以更精简,比如:
1、如下代码可以在each之外做共用:

var livelist = document.getElementById("livelist");

2、tap事件点击那里,可以挪到each之外,通过事件委托来实现

  • avst (作者)

    谢谢,但是在show.html页面中的并没有接到参数,并且都没有进入到window.addEventListener('liveID',function(event)中,因为我mui.alert(1);都不会出东西,这是什么原因

    2015-09-02 15:27

DCloud_UNI_CHB

DCloud_UNI_CHB

addEventListener写错了吧?没有中间的'a',你这个到底是事件绑定还是事件监听?

live.addEventListener('tap',function(e){  
    mui.fire(show,'liveID',{id:liveid});  
    mui.openWindow({  
        id:'show',  
        url:'show.html'  
    });  
});
avst

avst (作者)

非常感谢,传参成功。
自定义事件传递消息的目标webview必须是已经存在的,我的webview是不存在的所以报错。
要先创建一个webview 然后将事件绑定改为

live.addEventListener('tap',function(e){  
    console.log(live);  
    showPage = plus.webview.getWebviewById('show.html');                              
    mui.fire(showPage,'liveID',{id:liveid});  
    mui.openWindow({  
        id:'showPage',  
        url:'show.html'  
    });  
});
00飞00

00飞00 - 全栈 云原生

好荣幸啊。被邀请了!呵呵
成功就好,多看看demo和别人写的东西。
相信你会有收获的

  • avst (作者)

    非常感谢

    2015-09-02 18:25

该问题目前已经被锁定, 无法添加新回复