JS通过操作Element的Css来实现隐藏和显示(本示例只有隐藏)

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>Collapsed Form Elements</title>
    <style>
        .label{
            width: 400px;
            margin: 10px 0 0 0;
            padding: 10px;
            background-color: #ccccff;
            text-align: center;
            border: 1px solid #ccccff;
        }
        .elements{
            border: 1px solid #ccccff;
            padding: 10px;
            border: 1px solid #ccccff;
            width: 400px;
        }
        button{
            margin: 20px;
        }
    </style>
</head>
<body>
<form>
    <div>
        <div id="section1" class="label">
            <p>Checkboxes</p>
        </div>
        <br id="section1b" class="elements">
            <input type="checkbox" name="box1"/> - box one </br>
            <input type="checkbox" name="box1"/> - box one </br>
            <input type="checkbox" name="box1"/> - box one </br>
            <input type="checkbox" name="box1"/> - box one </br>
            <input type="checkbox" name="box1"/> - box one </br>
        </div>
    </div>
    <div>
        <div id="section2" class="label">
            <p>Buttons</p>
        </div>
        <div class="elements">
            <input type="radio" name="box1"/> - box one </br>
            <input type="radio" name="box1"/> - box one </br>
            <input type="radio" name="box1"/> - box one </br>
            <input type="radio" name="box1"/> - box one </br>
            <input type="radio" name="box1"/> - box one </br>
            <button>Submit</button>
        </div>
    </div>
</form>
<script type="text/javascript">

    /**
     * Css Visibility属性来隐藏和显示消息
     *
     * msg.style.hidden = "visible";   //显示
     * msg.style.hidden = "hidden";    //隐藏,但占用页面空间
     *
     * 或者CSS 的 display属性
     * msg.style.display = "block";  //显示
     * msg.style.display = "none";   //从显示状态删除
     *
     * display的4个属性
     * none: 该元素从显示中删除
     * block:元素当做一个块级元素来处理,在其前后都有换行
     * inline-block: 元素内容像一个块级元素一样格式化,然后向内联内容一样排列
     * inherit:(默认的显示),并且执行display属性继承至该元素的父节点
     *
     *
     * @type {NodeList}
     */

    var elements = document.getElementsByTagName("div");

    //折叠起所有的区段
    for(var i = 0; i <elements.length; i++){
        if(elements[i].className = "elements"){
            elements[i].style.display = "none";
        }else if(elements[i].className == "label"){
            elements[i].onclick = switchDisplay;
        }
    }

    //根据状态折叠或展开
    function switchDisplay(){
        var parent = this.parentNode;
        var target = parent.getElementsByTagName("div")[1];

        if(target.style.display == "none"){
            target.style.display = "block";
        }else{
            target.style.display = "none";
        }
        return false;
    }
</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值