首页 > 其他 > 详细

用闭包减少代码

时间:2014-05-21 12:11:18      阅读:424      评论:0      收藏:0      [点我收藏+]

 

做一个radiolist控制textbox显示隐藏的功能,最初的代码:

bubuko.com,布布扣
rblSecurityCameras = document.getElementsByName("<%=rblSecurityCameras.UniqueID %>");
        txtSecurityCameras = document.getElementById("<%=txtSecurityCameras.ClientID %>");


        for (i in rblSecurityCameras) {
            rblSecurityCameras[i].onclick = function(){
                if (this.value == "2" && this.checked) {
                    txtSecurityCameras.style.display = ‘none‘;
                }
                else if (this.value == "1" && this.checked) {
                    txtSecurityCameras.style.display = ‘‘;
                }
};
             if (rblSecurityCameras[i].value == "2" && rblSecurityCameras[i].checked) {
                    txtSecurityCameras.style.display = ‘none‘;
                }
                else if (rblSecurityCameras[i].value == "1" && rblSecurityCameras[i].checked) {
                    txtSecurityCameras.style.display = ‘‘;
                }
        }
bubuko.com,布布扣

点击事件和load的过程的代码是相同的,有代码洁癖的我就不干了。

使用闭包

bubuko.com,布布扣
 1 rblSecurityCameras = document.getElementsByName("<%=rblSecurityCameras.UniqueID %>");
 2         txtSecurityCameras = document.getElementById("<%=txtSecurityCameras.ClientID %>");
 3 
 4         var mm = function(obj, txt) {
 5             return (function() {
 6                 if (obj.value == "2" && obj.checked) {
 7                     txt.style.display = ‘none‘;
 8                 }
 9                 else if (obj.value == "1" && obj.checked) {
10                     txt.style.display = ‘‘;
11                 }
12             });
13         };
14 
15         for (i in rblSecurityCameras) {
16             rblSecurityCameras[i].onclick = mm(rblSecurityCameras[i], txtSecurityCameras);
17             mm(rblSecurityCameras[i], txtSecurityCameras)();
18         }
bubuko.com,布布扣

 

用闭包减少代码,布布扣,bubuko.com

用闭包减少代码

原文:http://www.cnblogs.com/405464904/p/3740484.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!