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
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞