鸟鸟
鸟鸟
  • 发布:2017-10-12 15:16
  • 更新:2017-10-12 16:13
  • 阅读:1171

仿照发布列表到详情页的例子,传递数据到子页失败

分类:MUI
mui

打算点击按钮,就将对应的数字传到子页中去。例如。渲染出来的第一个div中,点击按钮,就将数字“123”传到子页中去显示出来。第二个div,点击按钮,就将"456"传到子页中去显示出来。

但发现现在无论点击哪个div的按钮,都只是将“123”传递出去, 并不能做到对应按钮传递对应数字的功能。

新手上路,求各位指导。

father.html:

<div class="mui-content" id="father">  
  <div v-for="item in items">  
    <h1 id="text">这里是father.html</h1>  
     <span id="text2">{{item.a}}</span>  
  <div>  
     <button>{{item.go}}</button>  
  </div>  
</div>  

<script type="text/javascript">  
            mui.init();  

            var webview_son = null; //详情页webview  
            var titleNView = { //详情页原生导航配置  
                backgroundColor: '#f7f7f7', //导航栏背景色  
                titleText: '', //导航栏标题  
                titleColor: '#000000', //文字颜色  
                type: 'transparent', //透明渐变样式  
                autoBackButton: true, //自动绘制返回箭头  
                splitLine: { //底部分割线  
                    color: '#cccccc'  
                }  
            }  

            mui.plusReady(function() {  
                webview_son = mui.preload({  
                    url: 'son.html',  
                    id: 'son.html',  
                    styles: {  
                        "render":"always",  
                        "popGesture": "hide",  
                        "bounce": "vertical",  
                        "bounceBackground": "#efeff4",  
                        "titleNView": titleNView  
                    }  
                })  
            })  

            var father = new Vue({  
                el: '#father',  
                data: {  
                    items:[  
                        {"a": '123',"go": "点击进入son.html"},  
                        {"a": '456',"go": "点击进入son.html"},  
                        {"a": '789',"go": "点击进入son.html"}  
                    ]  
                }  
            })  

            mui('.mui-content').on('tap', 'button',function() {  
                var text = document.getElementById("text2").innerHTML.trim();  
                open_son(text);  
            });  

            function open_son(text) {  
                if(!webview_son) {  
                    setTimeout(function() {  
                        open_son(text);  
                    }, 100);  
                }  

                mui.fire(webview_son, 'get_son', {  
                    text: text  
                });  

                setTimeout(function () {  
                    webview_son.show("slide-in-right", 300);  
                },150);  
            }  
</script>

son.html:

<div class="mui-content">  
            <p>这是son.html</p>  

            <span>{{sonText}}</span>  
        </div>  

<script type="text/javascript">  
            mui.init();  

            function getDefaultData() {  
                return {  
                    sonText: ''  
                }  
            }  

            var vm = new Vue ({  
                el: '.mui-content',  
                data: getDefaultData(),  
                methods: {  
                    resetData: function() {//重置数据  
                        Object.assign(this.$data, getDefaultData());  
                    }  
                }  
            })  

            mui.plusReady(function() {  
                var localId = plus.webview.currentWebview().id;  

                document.addEventListener('get_son', function(event) {  
                    vm.sonText = event.detail.text;  
                })  
            });  

            mui.back = function() {  
                plus.webview.currentWebview().hide("auto", 300);  
                //动画结束后,恢复默认值  
                setTimeout(function() {  
                    window.scrollTo(0, 0);  
                    vm.resetData();  
                }, 300);  
            }  
</script>  
2017-10-12 15:16 负责人:无 分享
已邀请:
鸟鸟

鸟鸟 (作者)

自己已经解决问题。关键在于mui的on选择器。

在father.html的html代码中,我新增了一个类选择器"bodya"。

<div class="bodya" class="mui-content" id="father">  
  <div v-for="item in items">  
    <h1 id="text">这里是father.html</h1>  
     <span id="text2">{{item.a}}</span>  
  <div>  
     <button>{{item.go}}</button>  
  </div>  
</div>

然后将下面的“button”改为类名“.bodya”即可。

mui('.mui-content').on('tap', 'button',function() {  
                var text = document.getElementById("text2").innerHTML.trim();  
                open_son(text);  
            });

改为:

mui('.mui-content').on('tap', '.bodya',function() {  
                var text = document.getElementById("text2").innerHTML.trim();  
                open_son(text);  
            });
回梦無痕

回梦無痕 - 暂停服务

你的代码不严谨。
一:一个html的dom中id不能重复,你循环渲染了几个id一样的button,然后getElementById,肯定会出问题啦。
二:正确的写法是

mui('.mui-content').on('tap', 'button',function(e) {  
    var text = this.innerHTML;  
    open_son(text);  
});

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