码迷,mamicode.com
首页 > Web开发 > 详细

【JS学习笔记】关于选项卡,关于this,关于innerHTML

时间:2017-01-21 22:25:24      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:color   use   www   org   自定义属性   list   enter   title   input   

this:当前发生事件的元素。

简易的选项卡代码。思路,先把所有的都隐藏,再把当前的显示出来。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
#div1 .active{
    background:yellow;
    }
#div1 div{
    width:200px;
    height:200px;
    background:#ccc;
    border:1px solid #999;
    display:none;/*让所有div都看不到*/
    }
</style>
<script>
window.onload=function (){
    var oDiv=document.getElementById(‘div1‘);
    var aBtn=document.getElementsByTagName(‘input‘);
    var aDiv=oDiv.getElementsByTagName(‘div‘);
    
    for(var i=0;i<aBtn.length;i++)
    {
        aBtn[i].index=i;//循环的时候,先给每个button一个自定义属性index
        aBtn[i].onclick=function ()//给所有的按钮都加上onclick事件,在它点击某个按钮的时候发生以下函数
        {
            for(var i=0;i<aBtn.length;i++)
            {
                aBtn[i].className=‘‘;//在点击选项卡之前的一瞬间,先让所有按钮都没有class效果,也就是让class变空。
                aDiv[i].style.display=‘none‘;//在点击选项卡按钮之前一瞬间,先让所有div里面的元素隐藏了。
                }
            //alert(this.value);this:当前发生事件的元素
            this.className=‘active‘;
            aDiv[this.index].style.display=‘block‘;
            };
        }
    //总结思路,先把所有的都隐藏,再把当前的显示出来
    };
    
</script>
</head>

<body>
    <div id="div1">
        <input class="active" type="button" value="教育" />
        <input type="button" value="培训" />
        <input type="button" value="招生" />
        <input type="button" value="出国" />
        <div style="display:block;">1111</div>
        <div>2222</div>
        <div>3333</div>
        <div>4444</div>
    </div>
</body>
</html>

利用innerHTML做简易日历

css文件代码

@charset "utf-8";
/* CSS Document */

*{
    margin:0px;
    padding:0px;
    }

#tab{
    width:427px;
    height:780px;
    border:1px solid #000;
    margin:0 auto;
    }


        
ul{
    list-style:none;
    width:427px;
    }
li{
    cursor: pointer;
    float:left;
    width:100px;
    height:100px;
    background:#000;
    margin:10px;
    padding:10px;
    border:1px solid #000;
    color:#FFF;
    text-align:center;
    }
    
.active
{
    float:left;
    width:100px;
    height:100px;
    background:#FFF;
    margin:10px;
    padding:10px;
    border:1px solid #F00;
    color:#F00;
    text-align:center;
}

.text{
    clear:both;
    width:405px;
    height:200px;
    border:1px solid #000;
    margin:0 auto;
    text-align:center;
    }

html文件代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link rel="stylesheet" href="zns.css" type="text/css" />
<script>
window.onload = function ()
{
    var arr=[
        ‘快过年了,大家商量着去哪玩吧‘,
        ‘22222222222‘,
        ‘33333333333‘,
        ‘4444444444‘,
        ‘55555555‘,
        ‘666666666666666‘,
        ‘777777777777‘,
        ‘88888888888‘,
        ‘9999999999999‘,
        ‘10101010100101‘,
        ‘1111‘,
        ‘12121212‘
    ];
    var oDiv=document.getElementById(‘tab‘);
    var aLi=oDiv.getElementsByTagName(‘li‘);
    var oTxt=oDiv.getElementsByTagName(‘div‘)[0];
    
    for(var i=0;i<aLi.length;i++)
    {
        aLi[i].index=i;//给所有li加上index,可以用来取月份数
        aLi[i].onmouseover = function ()
        {
            for(var i=0;i<aLi.length;i++)
            {
                aLi[i].className=‘‘;//所有li清除active样式效果
                }
                this.className=‘active‘;
                
                oTxt.innerHTML=‘<h2>‘+(this.index+1)+‘月活动</h2><p>‘+arr[this.index]+‘</p>‘;
            };
        }
    };
</script>
</head>

<body>
<div id="tab" class="calendar">
    <ul>
        <li class="active"><h2>1</h2><p>JAN</p></li>
        <li><h2>2</h2><p>FER</p></li>
        <li><h2>3</h2><p>MAR</p></li>
        <li><h2>4</h2><p>APR</p></li>
        <li><h2>5</h2><p>MAY</p></li>
        <li><h2>6</h2><p>JUN</p></li>
        <li><h2>7</h2><p>JUL</p></li>
        <li><h2>8</h2><p>AUG</p></li>
        <li><h2>9</h2><p>SEP</p></li>
        <li><h2>10</h2><p>OCT</p></li>
        <li><h2>11</h2><p>NOV</p></li>
        <li><h2>12</h2><p>DEC</p></li>
    </ul>
    
    <div class="text">
        <h2>1月活动</h2>
        <p>快过年了,大家商量着去哪玩吧</p>
    </div>
</div>
</body>
</html>

【JS学习笔记】关于选项卡,关于this,关于innerHTML

标签:color   use   www   org   自定义属性   list   enter   title   input   

原文地址:http://www.cnblogs.com/zhy2017/p/6336691.html

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