如何将此脚本代码修改为最小行? [英] How to modify this script code to minimum line ?

查看:99
本文介绍了如何将此脚本代码修改为最小行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<script type="text/javascript">
       var hide1 = true;
       var hide2 = true;
       var hide3 = true;

       function hideColumn1(tableId, colIndex) {
           var table = document.getElementById(tableId);

           if (table != null) {
               for (i = 0; i < table.rows.length; i++) {
                   if (hide1)
                       table.rows[i].cells[colIndex - 1].style.display = 'none';
                   else
                       table.rows[i].cells[colIndex - 1].style.display = '';
               }
               hide1 = !hide1;
           }
       }
       function hideColumn2(tableId, colIndex) {
           var table = document.getElementById(tableId);

           if (table != null) {
               for (i = 0; i < table.rows.length; i++) {
                   if (hide2)
                       table.rows[i].cells[colIndex - 1].style.display = 'none';
                   else
                       table.rows[i].cells[colIndex - 1].style.display = '';
               }
               hide2 = !hide2;
           }
       }
       function hideColumn3(tableId, colIndex) {
           var table = document.getElementById(tableId);

           if (table != null) {
               for (i = 0; i < table.rows.length; i++) {
                   if (hide3)
                       table.rows[i].cells[colIndex - 1].style.display = 'none';
                   else
                       table.rows[i].cells[colIndex - 1].style.display = '';
               }
               hide3 = !hide3;
           }
       }



   </script>







在这个脚本中我使用3方法进行同样的工作。我找到了最好的解决方案。 plz给我正确的方法来完成这个脚本。




in this script i use 3 method for same work . i find the best solution . plz give me right way to complete this script.

推荐答案

如果你的意思是压缩你的代码:



If you mean compress your code to something like this:

function hideColumn1(e,t){var n=document.getElementById(e);if(n!=null){for(i=0;i<n.rows.length;i++){if(hide1)n.rows[i].cells[t-1].style.display="none";else n.rows[i].cells[t-1].style.display=""}hide1=!hide1}}function hideColumn2(e,t){var n=document.getElementById(e);if(n!=null){for(i=0;i<n.rows.length;i++){if(hide2)n.rows[i].cells[t-1].style.display="none";else n.rows[i].cells[t-1].style.display=""}hide2=!hide2}}function hideColumn3(e,t){var n=document.getElementById(e);if(n!=null){for(i=0;i<n.rows.length;i++){if(hide3)n.rows[i].cells[t-1].style.display="none";else n.rows[i].cells[t-1].style.display=""}hide3=!hide3}}var hide1=true;var hide2=true;var hide3=true





您可以尝试以下工具:http://jscompress.com/ [ ^ ]和 http://javascriptcompressor.com/ [ ^ ]。


除了boolean varible hide1,hide2&hide3之外,你在所有三个函数中基本上做同样的事情。从代码中我理解的是,你只是在变量中设置状态而不是在任何地方使用。如果是这种情况,你只能使用一个函数而不必为同一目的创建3个不同的函数
You are basically doing the same thing in all the three functions except for the boolean varible hide1, hide2 & hide3. from the code what i understand is that you are just setting the status in the variable and not used any where. if that is the case you can use only a single function and don't have to create 3 different functions for same purpose


<script type="text/javascript">
    var hide1 = true;
    var hide2 = true;
    var hide3 = true;

    function hideColumn1(tableId, colIndex,hideType,ColumnValue) {
        var table = document.getElementById(tableId);

        if (table != null) {
            for (i = 0; i < table.rows.length; i++) {
                if (hideType)
                    table.rows[i].cells[colIndex - 1].style.display = 'none';
                else
                    table.rows[i].cells[colIndex - 1].style.display = '';
            }
            if(ColumnValue == "Column1")
            {
                hide1 = !hideType;
            }
            if(ColumnValue == "Column2")
            {
                hide2 = !hideType;
            }
            if(ColumnValue == "Column3")
            {
                hide3 = !hideType;
            }
        }
    }


</script>





您只是隐藏列并更新变量hide1,hide2,hide3。所以我刚更新了你的功能。

1.在你的函数中加入更多参数。

2. hidetype传递变量的当前状态(true / false)

3. columnValue传递你的coumn id。你写的差异功能。

4.最后检查列值并根据设置变量状态。



希望它会有所帮助。



you are just hiding the column and updating the variable hide1,hide2,hide3. so i just updated the your function .
1. added to more parameter into your function.
2. hidetype pass the current status of variable (true/false)
3. columnValue pass the you coumn id. fot that you were written diff function.
4. at last check the column value and on the basis of that set the variable status.

hope it will be help.


这篇关于如何将此脚本代码修改为最小行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆