开始学习JS的一些基础知识。
跟着教程尝试做一些小的东西。
在这里进行记载。
1.for语句的使用。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>for语句</title>
<style>
div{width: 200px;height: 200px;float: left;border: 1px solid black;margin: 10px;}
</style>
<script>
window.οnlοad=function () {
var aDiv=document.getElementsByTagName('div');
for(var i=0;i<aDiv.length;i++){
aDiv[i].style.background='red';
}
};
</script>
</head>
<body>
<div></div>
<div></div>
<div></div>
<div></div>
</body>
</html>
这部分主要运用了for语句的循环。以及一些最基础的JS标签代码的使用。
2.全选,反选,全不选按钮。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>全选,反选</title>
<script>
window.οnlοad=function () {
var oBtn1=document.getElementById('btn1');
var oDiv=document.getElementById('div1');
var aCh=oDiv.getElementsByTagName('input');
var oBtn2=document.getElementById('btn2');
var oDiv=document.getElementById('div1');
var aCh=oDiv.getElementsByTagName('input');
var oBtn3=document.getElementById('btn3');
var oDiv=document.getElementById('div1');
var aCh=oDiv.getElementsByTagName('input');
//全选
oBtn1.οnclick=function () {
for(var i=0;i<aCh.length;i++){
aCh[i].checked=true;
}
};
//全不选
oBtn2.οnclick=function () {
for(var i=0;i<aCh.length;i++){
aCh[i].checked=false;
}
};
//反选
oBtn3.οnclick=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/>
</div>
</body>
</html>
这一部分用到了true和false的应用。还有IF语句的出现。
3.选项卡。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>选项卡</title>
<style>
#div1 .active{background: yellow;}
#div1 div{width: 200px;height: 200px;background: #ccc;border: 1px solid #999;display: none;}
</style>
<script>
window.οnlοad=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].index=i;//Div显示块的切换
aBtn[i].οnclick=function () {
for(var i=0;i<aBtn.length;i++){
aBtn[i].className="";//当class为空的时候,无颜色。
aDiv[i].style.display='none';
}
this.className='active';//当class为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;">11</div>
<div>22</div>
<div>33</div>
<div>44</div>
</div>
</body>
</html>
这一部分对标签 className和index使用,其中index这个自定义标签应加入到JS代码中,如果加入到结构层中会被自动忽略。
4.简易日历的制作。
了解了(innerHTML ) 即 获取对象的内容 或 向对象插入内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>简易日历</title>
<style>
body,ul,li,h2,p
{
margin:0px;
padding:0px;
}
#tab{
width: 226px;
height: 400px;
border: 1px solid black;
margin: 80px auto;
background-color: #eee;
}
#tab .active{
background-color: #eee;
color: red;
}
#tab ul li{
width: 60px;
height:60px;
float: left;
margin-top:10px;
margin-left: 10px;
border: 1px solid black;
background-color: #0E1418;
color: #eee;
list-style-type:none;
text-align: center;
}
#tab .text{
width: 205px;
height:90px;
float: left;
margin-top:10px;
margin-left: 10px;
border: 1px solid black;
}
#tab .text h2{
font-size: 30px;
}
#tab .text p{
line-height: 40px;
}
</style>
<script>
window.οnlοad=function () {
var arr=[
'新年春节假期来了!',
'二月',
'三月',
'4月',
'5月',
'6月',
'7月',
'8月',
'9月',
'10月',
'11月',
'12月'
];
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;
aLi[i].οnmοuseοver=function () {
for(var i=0;i<aLi.length;i++){
aLi[i].className='';
}
this.className='active';
//innerHTML 即 获取对象的内容 或 向对象插入内容
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">
</div>
</div>
</body>
</html>