开发者社区 问答 正文

HTML代码修改了以后就没有效果,jQuery部分要怎么改?

【jQuery代码】
screenshot
【原HTML代码】
screenshot
【修改部分HTML代码】——之后就没有反应了,请问jQuery部分要怎样修改
screenshot

展开
收起
吴孟桥 2016-06-05 18:23:14 2828 分享 版权
1 条回答
写回答
取消 提交回答
  • 代码要依据结构来修改,结构改了当然没有效果

     <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js"></script>
    <ul class="categories">
        <li>
            <h1><input type="checkbox" /><i class="icon checkbox"></i><a href="#">数码电子</a></h1>
            <ul>
                <li><input type="checkbox" />手机</li>
                <li><input type="checkbox" />电脑</li>
                <li><input type="checkbox" />U盘</li>
                <li><input type="checkbox" />相机</li>
            </ul>
        </li>
        <li>
            <h1><input type="checkbox" /><i class="icon checkbox"></i><a href="#">服装</a></h1>
            <ul>
                <li><input type="checkbox" />毛衣</li>
                <li><input type="checkbox" />T恤</li>
                <li><input type="checkbox" />羽绒服</li>
                <li><input type="checkbox" />秋裤</li>
            </ul>
        </li>
    </ul>
    <script type="text/javascript">
        $(function () {
            $("ul.categories input").click(function () {
                var children = $(this).parent().next();
                var isChecked = $(this).prop("checked");
                var isParent = false;
                if (children.is('ul')) {
                    if (isChecked) {
                        children.find("input").prop("checked", "checked");
                    } else {
                        children.find("input").removeProp("checked");
                    }
                    return///顶级checkbox不需要执行下面
                }
                var checkedCount = $(this).parent().parent().find(">li >input:checked").length;
                var count = $(this).parent().parent().find(">li >input").length;
    
                if (checkedCount == count) {
                    $(this).parent().parent().parent().find(">h1 input").prop("checked", "checked");
                } else {
                    $(this).parent().parent().parent().find(">h1 input").removeProp("checked");
                }
    
            });
        });
    </script>
    2019-07-17 19:27:26
    赞同 展开评论