14天入门JavaScript-day two

前面一天学习用js实现简单的网页效果,现在来学学js的相关

JavaScript组成

  • ECMAScript:解释器、翻译

  • DOM:Document Object Model 操作HTML的能力

  • BOM:Browser Object Model 浏览器兼容

demo1

字面量(常量)
变量:

var a=?

样式优先级:

*<标签<class<ID<行间

<script>alert(abc);</script>

demo2

div改变样式

<style>
    #div1 {
        width: 100px;
        height: 100px;
        background: red;
    }
    
    #div2 {
        width: 100px;
        height: 100px;
        background: red;
    }
</style>
    <script>
        function setColor(color)
        {
            var oDiv=document.getElementById('div1');
            oDiv.style.background=color;
        }
    </script>
    <input type="button" value="变绿" onclick="setColor('green');" />
    <div id="div1"></div>

demo3

获取class

<style>
div {width:200px; height:200px; float:left; border:1px solid black; margin:10px;}
</style>
<script>
window.onload=function ()
{
    var aDiv=document.getElementsByTagName('div');
    
    //alert(aDiv.length);
    //aDiv.style.background='red';
    
    for(var i=0;i<aDiv.length;i++)
    {
        //i->0,1,2,3
        aDiv[i].style.background='red';
    }

};
</script>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>

demo4

最简单选项卡

<style>
#div1 .active {background:yellow;}
#div1 div {width:200px; height:200px; background:#CCC; border:1px solid #999; display:none;}
</style>
<script>
window.onload=function ()
{
    var oDiv=document.getElementById('div1');
    var aBtn=oDiv.getElementsByTagName('input');
    var aDiv=oDiv.getElementsByTagName('div');
    
    for(var i=0;i<aBtn.length;i++)
    {
        aBtn[i].onclick=function ()
        {
            for(var i=0;i<aBtn.length;i++)
            {
                aBtn[i].className='';
                aDiv[i].style.display='none';
            }
            this.className='active';
            //alert(this.index);
            aDiv[this.index].style.display='block';
        };
    }
};
</script>
<div id="div1">
    <input class="active" type="button" value="教育" index="0" />
    <input type="button" value="培训" index="1" />
    <input type="button" value="招生" index="2" />
    <input type="button" value="出国" index="3" />
    <div style="display:block;">1111</div>
    <div>2222</div>
    <div>333</div>
    <div>4444</div>
</div>

demo5

for循环的理解

<script>
for(var i=0;i<5;i++)
{
    alert(i);
}
</script>

demo6

inner HTML 的使用简单理解

#div1 {width:200px; height:150px; border:1px solid black;}
<script>
window.onload=function ()
{
    var oTxt=document.getElementById('txt1');
    var oBtn=document.getElementById('btn1');
    var oDiv=document.getElementById('div1');
    
    oBtn.onclick=function ()
    {
        oDiv.innerHTML=oTxt.value;
        //alert(oDiv.innerHTML);
    };
};
</script>
<input id="txt1" type="text" />
<input id="btn1" type="button" value="设置文字" />
<div id="div1">
</div>

源码查看

查看原文

    原文作者:lzhnu
    原文地址: https://segmentfault.com/a/1190000007443055
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞