PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > HBuilderX教程 > 正文

    hbuilder mui HTML5 WebApp开发事件绑定

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:在做公司项目的时候,有一个页面有个个数不定的勾选框checkbox视图区,每个勾选框对应一个ID和NAME的值,我们需要把选中的chexkbox的ID获取...
    在做公司项目的时候,有一个页面有个个数不定的勾选框checkbox视图区,每个勾选框对应一个ID和NAME的值,我们需要把选中的chexkbox的ID获取到,然后以拼接成字符串,字符中间用“,”分割。在这个需求里面有三个点,1:不定选项的checkbox; 2:获取选中的checkbox的ID; 3:字符串中间使用“,”拼接。 

    关于上面的三个知识点来说,个人任我最难的是第2个,难点在在使用H5做的时候我还不知道js的时间绑定中的这种方法,我们虽说是要获取选中的checkbox的ID,实际这些需要在checkbox的点击时间里面去处理。以前做原生的时候是在button的点击事件里面处理的,每个button的tag值我们可以设置为0~n,然后根据tag值从数据源数组里面去取数据。但是在H5里面去不能这样处理,使用下面的事件绑定方法:

    document.getElementById('imageView').addEventListener('tap',function(){})
    • 1
    • 1

    这个事件绑定的方式有个特点就是需要知道每个checkbox的id,然后一个id有一个事件绑定方法。显然这么做是难以接受的,因为checkbox的个数不定。 
    还有一点就是,保存选中checkbox的ID,再加上每个checkbox都存在多次点击的时候,这个时候就需要在存储ID的容器里面处理选中的时候存储的是ID,非选中的时候存储的不是ID。想到每个ID的唯一性和字典里面key的唯一性,下面的就好做了。每一个checkbox对应字典里面的一个键值对,key是ID,选中时的value是ID,非选中的时候value不是ID(这里我们假定为是“X”);然后在checkbox点击事件里面,当checkbox被选中时,设置其对应的键值对的value为对应的ID,否则设置为“X”。 
    这里还是没有说到这篇博客的重点,事件绑定。其实这也是这么一个功能点,我认为对我来说我需要学习的一个知识点,所以我叫他事件绑定。

    js事件绑定

    《JavaScript事件绑定》 
    《Javascript事件绑定的几种方式》 
    关于JavaScript的事件绑定,可以看看上面两篇文章。但在实际使用中我们需要多看看mui提供的方式,因为平台和环境不一样。

    代码

    1. <html> 
    2.     <head> 
    3.         <meta charset="utf-8"
    4.         <title>Hello MUI</title> 
    5.         <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no"
    6.         <meta name="apple-mobile-web-app-capable" content="yes"
    7.         <meta name="apple-mobile-web-app-status-bar-style" content="black"
    8.         <!--标准mui.css--> 
    9.         <link rel="stylesheet" href="../css/mui.min.css"
    10.         <link rel="stylesheet" href="../css/mui.css" /> 
    11.         <!--App自定义的css--> 
    12.         <style type="text/css"
    13.             .mui-checkbox{ 
    14.                 height: 44px; 
    15.                 background: white; 
    16.                 border-bottom-style: solid; 
    17.                 border-bottom-color: #F3F3F3; 
    18.                 border-bottom-width: 1px; 
    19.             } 
    20.             .span { 
    21.                 float: right; 
    22.                 margin-right: 150px; 
    23.                 height: 44px; 
    24.                 line-height: 44px; 
    25.             } 
    26.             .checkbox { 
    27.                 margin-top: 5px; 
    28.                 margin-right: 200px; 
    29.                 height: 44px; 
    30.                 line-height: 44px; 
    31.             } 
    32.             .checkbox :before { 
    33.                 0px; 
    34.             }    
    35.         </style> 
    36.     </head> 
    37.     <body> 
    38.         <header class="mui-bar mui-bar-nav"
    39.             <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a> 
    40.             <h1 class="mui-title">事件绑定</h1> 
    41.         </header> 
    42.         <div class="mui-content"
    43.             <div class="mui-checkbox"
    44.                 <span class="span">我的id:1</span> 
    45.                 <input type="checkbox" class="checkbox" id="" value="" /> 
    46.             </div> 
    47.             <div class="mui-checkbox"
    48.                 <span class="span">我的id:2</span> 
    49.                 <input type="checkbox" class="checkbox" id="" value="" /> 
    50.             </div> 
    51.             <div class="mui-checkbox"
    52.                 <span class="span">我的id:3</span> 
    53.                 <input type="checkbox" class="checkbox" id="" value="" /> 
    54.             </div> 
    55.         </div> 
    56.     </body> 
    57.     <script src="../js/mui.min.js"></script> 
    58.     <script src="../js/mui.zoom.js">    </script> 
    59.     <script src="../js/mui.previewimage.js"></script> 
    60.     <script src="../js/tools.js" ></script> 
    61.     <script> 
    62.  
    63.         var checkDice = new Array(); 
    64.  
    65.         mui.init({ 
    66.             swipeBack:true //启用右滑关闭功能 
    67.         }); 
    68.  
    69.         mui.plusReady(function() {   
    70.             getData(); 
    71.         }) 
    72.         // 数据处理 
    73.         function getData() { 
    74.             var content = document.body.querySelector('.mui-content'); 
    75.             content.innerHTML = '';  
    76.             var count = Math.random()*10 | 0; // 获取0~10之间的整数 
    77.             count = count + 5; // 6~16之间 
    78.             console.log(count); 
    79.             for (var index = 0; index < count; index ++ ) { 
    80.                 var div = document.createElement('mui-checkbox'); 
    81.                 div.innerHTML = '<div class="mui-checkbox">' + '<span class="span">我的ID:' + index + '</span>' + '<input type="checkbox" class="checkbox" id="' + index +'" value="" /></div>'
    82.                 content.appendChild(div,content.firstChild);     
    83.                 checkDice[String(index)] = 'X'
    84.             }  
    85.         }  
    86.         // 事件绑定 
    87.         mui('.mui-content').on('change','input',function(){ 
    88.             console.log("id = " + this.id + " status = " + this.checked); 
    89.             if (this.checked) {  
    90.                 checkDice[this.id] = this.id; 
    91.             } else
    92.                 checkDice[this.id] = 'X'
    93.             } 
    94.             console.log("checkDice = " + checkDice); 
    95.             console.log('被选中的checkbox的ID是:',makeData(checkDice)); 
    96.         }) 
    97.         // 数据处理 www.bcty365.com B5教程网 
    98.         function makeData(dice){ 
    99.             var result = ''
    100.             for (var key in dice) { 
    101.                 if (dice[key] != 'X') { 
    102.                     result = result + dice[key] + ','
    103.                 }  
    104.             } 
    105.             // 去除字符串最后的','后返回 
    106.             return result.substring(0,result.length - 1); 
    107.         } 
    108.     </script> 
    109. </html> 

    总结

     

    写代码前,理清思路,学会多看文档,多思考。

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-146-4784-1.html
    相关热词搜索: mui hbuilder HTML5 事件绑定