js学习笔记-初探JavaScript魅力 – 2

2013年10月09日 | 分享 | 6条评论

JS练习-合并参数-行间事件-提取行间事件

<!DOCTYPE html>  
<html>  
<head>  
    <title>JS练习-合并参数-行间事件-提取行间事件</title>  
    <meta charset="UTF-8" />  
    <style>  
    #div1{width: 200px;height: 200px;background: red;}  
    #div2{width: 200px;height: 200px;border: 1px solid black;}  
    .box{background: red}  
    </style>  
    <script>  
    //最原始的写法  
    function toGreen()  
    {  
        var oDiv=document.getElementById('div1');  
        oDiv.style.background='green';  
    }  
    function toYellow()  
    {  
        var oDiv=document.getElementById('div1');  
        oDiv.style.background='yellow';  
    }  
    function toBlack()  
    {  
        var oDiv=document.getElementById('div1');  
        oDiv.style.background='black';  
    }  
  
    //单个参数  
    function setColor(color)  
    {  
        var oDiv=document.getElementById('div1');  
        oDiv.style.background=color;  
    }  
  
    //两个参数  
    function setStyle(name,value)  
    {  
        var oDiv=document.getElementById('div1');  
        /*oDiv.style.name=value;  这里会出错*/  
        oDiv.style[name]=value;  
        //oDiv[style][name]=value;  这个和上面效果是一样的  
    }  
  
  
  
  
    //1.style加样式  行间  
    //2.style去样式  行间  
    //3.建议只修改class或者只修改style  
  
    //通过改变class来控制背景色  
    function toRed1()  
    {  
        var oDiv=document.getElementById('div2');  
        oDiv.className='box';  
    }  
    //通过改变行间样式来改变背景色  
    function toGreen1()  
    {  
        var oDiv=document.getElementById('div2');  
        oDiv.style.background='green';  
    }  
  
      
    </script>  
</head>  
<body>  
    <input type="button" value="变黄" onclick="toYellow();"/>  
    <input type="button" value="变绿" onclick="toGreen();"/>  
    <input type="button" value="变黑" onclick="toBlack();"/>  
    <br>  
    <input type="button" value="变黄" onclick="setColor('yellow');"/>  
    <input type="button" value="变绿" onclick="setColor('green');"/>  
    <input type="button" value="变黑" onclick="setColor('black');"/>  
    <br/>  
    <input type="button" value="变宽" onclick="setStyle('width','400px');"/>  
    <input type="button" value="变高" onclick="setStyle('height','400px');"/>  
    <input type="button" value="变绿" onclick="setStyle('background','green');"/>  
    <div id="div1"></div>  
    <br>  
    <br>  
    <input type="button" value="变红" onclick="toRed1();">  
    <input type="button" value="变绿" onclick="toGreen1();">  
    <div id="div2"></div>  
</body>  
</html>  

JS练习-提取行间事件,获取一组元素,循环

<!DOCTYPE html>  
<html>  
<head>  
    <title>JS练习-提取行间事件,获取一组元素,循环</title>  
    <meta charset="UTF-8" />  
    <script>  
  
          
        window.onload=function()  
        //在加载完整个页面后执行,如果不添加window.onload,则变量获取不到id tagname  
        {  
            //获取一组元素 getElementsByTagName  
            var aDiv=document.getElementsByTagName('div');  
  
            var bDiv=document.getElementById('btn2');  
  
            //alert(aDiv.lenth);  
  
            //aDiv.style.background='red';这个是错误的  
  
            /*  
            bDiv.onclick=function()  
            {  
                aDiv[0].style.background='red';  
                aDiv[1].style.background='red';  
                aDiv[2].style.background='red';  
                aDiv[3].style.background='red';  
            };  
            */  
  
            bDiv.onclick=function()  
            {  
                for (var i=0;i<aDiv.length; i++)   
                //判断aDiv的个数 原i<4  
                {  
                    aDiv[i].style.background='red';  
                };  
            };  
              
        }  
  
        //while循环  
        /*  
        var i=0;        //1.初始化  
        while (i<5)     //2.条件  
        {  
            alert(i);   //3.语句  
            ii=i+1;      //4.自增 也可以用 i++  
        }  
        */  
  
        //for循环  
        /*  
        for(var i=0;i<5;i++)  
        {  
            alert(i);  
        }  
        */  
  
    </script>  
    <style>  
        div{width: 200px;height: 200px;border: 1px solid black;margin: 10px;float: left;}  
    </style>  
</head>  
<body>  
    <input type="button" id="btn2" value="变色"/>  
    <br/>  
    <div></div>  
    <div></div>  
    <div></div>  
    <div></div>  
</body>  
</html>  

全选,不选,反选

<!DOCTYPE html>  
<html>  
<head>  
    <title>全选,不选,反选</title>  
    <meta charset="UTF-8" />  
    <script>  
    window.onload=function()  //此处是“=”不是"."
    {  
        var oBtn1=document.getElementById('btn1');  
        var oBtn2=document.getElementById('btn2');  
        var oBtn3=document.getElementById('btn3');  
        var oDiv=document.getElementById('div1');  
        var aCh=oDiv.getElementsByTagName('input');  
        oBtn1.onclick=function()  
        {  
            //aCh[0].checked=true;  //好吧checked别写错了 纠结  
            for (var i=0; i<aCh.length; i++) {  
                aCh[i].checked=true;  
            };  
        };  
  
        oBtn2.onclick=function()  
        {  
            for (var i=0; i<aCh.length; i++)  //注意别写漏var,及i=0不是=1
            {  
                aCh[i].checked=false;  
            };  
        };  
  
        oBtn3.onclick=function()  
        {  
            for (var i=0; i<aCh.length; i++)  
            {  
                if (aCh[i].checked==true)//别忘了用==判断,且后面没分号  
                {  
                    aCh[i].checked=false;  
                }  
                else  
                {  
                    aCh[i].checked=true;  
                }  
                  
            };  
  
        };  
  
  
    };  
    </script>  
</head>  
<body>  
    <input id="btn1" type="button" value="全选" /><br/>  
    <input id="btn2" type="button" value="不选" /><br/>  
    <input id="btn3" type="button" value="反选" /><br/>  
    <div id="div1">  
        <input type="checkbox"/><br/>  
        <input type="checkbox"/><br/>  
        <input type="checkbox"/><br/>  
        <input type="checkbox"/><br/>  
        <input type="checkbox"/><br/>  
        <input type="checkbox"/><br/>  
        <input type="checkbox"/><br/>  
        <input type="checkbox"/><br/>  
        <input type="checkbox"/><br/>  
        <input type="checkbox"/><br/>  
        <input type="checkbox"/><br/>  
    </div>  
</body>  
</html>  

选项卡制作

<!DOCTYPE HTML>  
<html>  
<head>  
<meta charset="utf-8">  
<title>选项卡</title>  
<style>  
    input.active{background: red}  
    #div1 div{width:200px;height:200px;background:red;display: none;}  
</style>  
<script>  
window.onload=function()  
{  
    var oBtn=document.getElementsByTagName('input');  
    var oDiv=document.getElementById('div1');  
    var aCh=oDiv.getElementsByTagName('div');  
  
    for(var i=0;i<oBtn.length;i++)  
    {  
        oBtn[i].index=i;//注意此处的引用  
        oBtn[i].onclick=function()  
        {  
            for(var i=0;i<oBtn.length;i++){  
                //先去掉之前的属性  
                oBtn[i].className='';  
                aCh[i].style.display='none';  
            }  
            //this的用法  
            this.className='active';  
            aCh[this.index].style.display='block';  
        };  
    };  
};  
</script>  
</head>  
  
<body>  
<input type="button" value="选项卡1" class="active">  
<input type="button" value="选项卡2">  
<input type="button" value="选项卡3">  
<br>  
<div id="div1">  
    <div style="display:block">内容1</div>  
    <div>内容2</div>  
    <div>内容3</div>  
</div>  
</body>  
</html>  

万年历制作(好吧,我承认下面内容中的div结构,css都是复制粘贴的,当然 js都是我手打并测试的...)

<!DOCTYPE HTML>  
<html>  
<head>  
<meta charset="utf-8">  
<title>万年历</title>  
<style>  
    * { padding: 0; margin: 0; }  
    li { list-style: none; }  
    body { background: #f6f9fc; font-family: arial; }  
    .calendar { width: 210px; margin: 50px auto 0; padding: 10px 10px 20px 20px; background: #eae9e9; }  
    .calendar ul { width: 210px; overflow: hidden; padding-bottom: 10px; }  
    .calendar li { float: left; width: 58px; height: 54px; margin: 10px 10px 0 0; border: 1px solid #fff; background: #424242; color: #fff; text-align: center; cursor: pointer; }  
    .calendar li h2 { font-size: 20px; padding-top: 5px; }  
    .calendar li p { font-size: 14px; }  
    .calendar .active { border: 1px solid #424242; background: #fff; color: #e84a7e; }  
    .calendar .active h2 { }  
    .calendar .active p { font-weight: bold; }  
    .calendar .text { width: 178px; padding: 0 10px 10px; border: 1px solid #fff; padding-top: 10px; background: #f1f1f1; color: #555; }  
    .calendar .text h2 {font-size: 14px; margin-bottom: 10px; }  
    .calendar .text p { font-size: 12px; line-height: 18px; }  
</style>  
<script>  
window.onload=function()  
{  
    //这里是赋值数组,方便后续调用  
    var aDatas=[  
        "快过年了,大家可以商量着去哪玩吧~",  
        "精通JavaScript开发课程 - 结课标准 - 有十条标准可让大家修练成JS高手……",  
        "HTML5开发课程,让你熟练掌握移动应用开发技术",  
        "精通各种DOM类应用,熟练掌握面向对象编程思想(OOP)、熟悉JS面向对象开发方式",  
        "熟练掌握AJAX技术及相关应用(例如:新浪微博级应用)",  
        "可以独立写出类似jQuery的小型库(支持各类选择符、事件类、DOM、自定义动画animate、AJAX……)",  
        "精通JS运动特效技术,能完整实现各类网站所有动画特效,如 智能课堂 官网",  
        "掌握JS调试及优化技术、能兼容所有浏览器",  
        "精通JS事件对象及事件的工作机制,并能完成各类跨平台应用模块的开发",  
        "能独立开发表现和性能都很优秀的RIA应用",  
        "了解后台编程的相关知识,能够和后台工程师配合完成大型交互应用",  
        "熟悉正则表达式的编写、应用及高级表单验证技术"  
    ];  
  
    var oDiv=document.getElementById('tab');  
    var aLi=oDiv.getElementsByTagName('li');  
  
    var oTxt=oDiv.getElementsByTagName('div')[0]; //注意第0个的应用  
    //var oTxt=document.getElementById('txt'); 也可以这么写,简单点  
  
    for(i=0;i<aLi.length;i++)  
    {  
        aLi[i].index=i;  
        aLi[i].onclick=function()//这里别漏了[i]  
        {  
            for(i=0;i<aLi.length;i++)  
            {  
                aLi[i].className='';  
            }  
            this.className='active';  
  
            //这个是在onclick里面的,别写出外面  
            oTxt.innerHTML='<h2>'+(this.index+1)+'月活动</h2><p>'+aDatas[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" id="txt">  
            <h2>1月活动</h2>  
            <p>快过年了,大家可以商量着去哪玩吧~</p>  
        </div>  
  
    </div>  
</body>  
</html>  

6 条评论

        1. 其实我是在网页云课堂看的 里面有视屏教程 你可以去看看 更加适合 我这边只是自己做了一些测试还有笔记

发布评论