猿问

以下js应该怎样封装成fun(id,name){}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="jquery.min.js"></script>
</head>
<body>
<table id="setPermission" class="table marginTop_30">
    <tr>
        <td colspan="5">
            <span class="font_bold margin_X10">角色管理</span>
            <input type="checkbox" id="selectAllTerm">
            <label for="selectAllTerm">全选</label>
        </td>
    </tr>
    <tr id="termRow">
        <td>
            <input type="checkbox" name="chTerm" id="dd">
            <label>查看学期列表</label>
        </td>
        <td>
            <input type="checkbox" name="chTerm">
            <label>添加学期</label>
        </td>
        <td>
            <input type="checkbox" name="chTerm">
            <label>修改学期</label>
        </td>
        <td>
            <input type="checkbox" name="chTerm">
            <label>删除学期</label>
        </td>
        <td>
            <input type="checkbox" name="chTerm">
            <label>停用/启用学期</label>
        </td>
    </tr>
    <tr>
        <td colspan="5">
            <span class="font_bold margin_X10">专业管理</span>
            <input type="checkbox" id="selectAllSpecialty">
            <label for="selectAllSpecialty">全选</label>
        </td>
    </tr>
    <tr id="specialtyRow">
        <td>
            <input type="checkbox" name="chAllSpecialty">
            <label>查看专业列表</label>
        </td>
        <td>
            <input type="checkbox" name="chAllSpecialty">
            <label>添加专业</label>
        </td>
        <td>
            <input type="checkbox" name="chAllSpecialty">
            <label>修改专业</label>
        </td>
        <td>
            <input type="checkbox" name="chAllSpecialty">
            <label>删除专业</label>
        </td>
        <td>
            <input type="checkbox" name="chAllSpecialty">
            <label>停用/启用专业</label>
        </td>
    </tr>
</table>

<script>
     var checkList=$("input[name='chTerm']");
    function selectAll (id) {
        $(id).click(function () {
            checkList.prop("checked", $(id).prop("checked"));
            hasAll();
        });
    };
    function hasAll(){

        var chknum = checkList.length;
        var Count=0;
        for(var i=0;i<chknum;i++){
            if(checkList[i].checked){
                Count++;
            }
        }
        console.log(Count);
        if(chknum==Count){//全选
            $("#selectAllTerm").prop("checked",true);
        }else{//不全选
            $("#selectAllTerm").prop("checked",false);
        }
    }
    selectAll(selectAllTerm);
    selectAll(selectAllSpecialty);
$(function(){
    $("input[name='chTerm']").each(function(){
        $(this).click(function(){
            hasAll();
        })
    })
})
</script>
</body>
</html>


qq_sU_4
浏览 1592回答 2
2回答

宝慕林2233867

封装函数主要就是写一个function,函数体写在函数里面就形成函数了,把经常改的东西变为参数就好了,然后最后用的时候调用一次就好了。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答