这部分基本与Java一致
~~~
<script>
var i = 0;
i++;
if(i === 5){
i = 0;
}
i%=5;
</script>
~~~
这里出现了===,我们称为全等,它与==的区别就是,==之比较值,不比较类型,而===必须值和类型完全对应才返回true
课后练习-1:li循环给不同的颜色(例如:红黄蓝--红黄蓝--红黄蓝....),鼠标移入颜色变化,鼠标移开颜色返回
方案一:索引值
方案二:字符串记录颜色
~~~
<script>
window.onload = function () {
var aLi = document.getElementsByTagName('li');
var arr = ['red','yellow','blue'];
var str = '';
for(var i = 0;i < aLi.length;i++){
aLi[i].index = i;
aLi[i].style.background = arr[i%arr.length];
aLi[i].onmouseover = function () {
str = this.style.background;
this.style.background = '#ccc';
}
aLi[i].onmouseout= function () {
this.style.background = str;
//this.style.background = arr[this.index%arr.length];
}
}
}
</script>
~~~
课后练习-2:针对上个练习进行拓展,点击选中是黑色,移开也是黑色,再点击其他,新点的是黑色,上次的黑色还原
~~~
<script>
window.onload = function () {
var aLi = document.getElementsByTagName('li');
var arr = ['red', 'yellow', 'blue'];
var str = '';//记录旧颜色
for (var i = 0; i < aLi.length; i++) {
aLi[i].index = i;
aLi[i].active = false;
aLi[i].style.background = arr[i % arr.length];
aLi[i].onmouseover = function () {
if(!this.active){
str = this.style.background;
}
this.style.background = '#ccc';
}
aLi[i].onmouseout = function () {
if(!this.active){
this.style.background = str;
}else{
this.style.background = '#000';
}
//this.style.background = arr[this.index%arr.length];
}
aLi[i].onclick = function () {
for(var i = 0;i < aLi.length;i++){
if(aLi[i].active){
aLi[i].active = false;
aLi[i].style.background =arr[i%arr.length];
}
}
this.active = true;
this.style.background = '#000';
}
}
}
</script>
~~~
***
### 逻辑与&&、或||、非!
与Java完全一致
***
课后作业-1:反选小实例
~~~
<script>
window.onload = function () {
var aInp = document.getElementsByTagName('input');
// 加checked是可以的,但也有其他办法
// oInp[1].checked = false;
aInp[0].onclick = function () {
for(var i = 1;i < aInp.length;i++){
// if(aInp[i].checked){
// aInp[i].checked = false;
// }else{
// aInp[i].checked = true;
// }
aInp[i].checked = !aInp[i].checked;
}
}
}
</script>
~~~
***
拓展练习-1:
![](https://box.kancloud.cn/b4f6c990c34a3fc6772370abc1fd608c_504x63.gif)
拓展练习-2:
![](https://box.kancloud.cn/816f460a945ca7ff9f8e8090f7d72d3b_312x218.gif)