码迷,mamicode.com
首页 > 编程语言 > 详细

javascript 简易选项卡

时间:2019-09-20 23:18:18      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:内容   spl   tle   存储   index   his   elements   oat   body   

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css" >
        .xuanx{
            width: 600px;
            height: 300px;
            background-color: red;
        }
        .clearfix:after{
            content: ‘‘;
            visibility:hidden;
            height: 0;
            clear:both;
            display: block;
        }
        .nav a{
            color: #fff;
            text-decoration: none;
            height: 30px;
            width: 200px ;
            line-height: 30px;
            
            text-align:center;
            display: inline-block;
            background-color: #000;
            float: left;
        }
        .content{
            width: 600px;
            height: 270px;
            background-color: blue;
        }
        .content div{
            display: none;
        }
    </style>
</head>
<body>
    <div class="xuanx">
        <div class="nav clearfix " id = ‘link‘>
            <a href="javascript:void(0)">标题1</a>
            <a href="javascript:void(0)">标题2</a>
            <a href="javascript:void(0)">标题3</a>
        </div>
        <div class="content" id = ‘link-centent‘>

        <div style=‘display: block;‘>
            <span>内容1</span>
        </div>
        <div >
            <span>内容2</span>
        </div>
        <div >
            <span>内容3</span>
        </div>
            
        </div>
    </div>

    <script type="text/javascript">
    var Aparent = document.getElementById(link);
    var Achild = Aparent.getElementsByTagName(a);
    var Div = document.getElementById(link-centent);
    var Divs = Div.getElementsByTagName(div);
    for(let i = 0;i<Achild.length;i++){

        // Achild[i].index = i;    这里是用var的解决方法,将每一次循环的索引进行存储,后面进行直接调用
        Achild[i].onclick = function(){

            for(var j=0;j<Achild.length;j++){
                Achild[j].style=‘‘;
                Divs[j].style=‘‘;
            }
            this.style.color= red;
            this.style.backgroundColor=blue;
            
            
            
            // Divs[this.index].style.display=‘block‘;
            Divs[i].style.display=block;
        }
    
    }

    </script>
</body>
</html>

 

javascript 简易选项卡

标签:内容   spl   tle   存储   index   his   elements   oat   body   

原文地址:https://www.cnblogs.com/whileke/p/11560201.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!