Blogger Information
Blog 20
fans 1
comment 2
visits 16800
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
把JS数组相关函数案例、常用事件案例练习一遍
月迎下弦的博客
Original
1005 people have browsed it

4. 把JS数组相关函数案例、常用事件案例练习一遍

if

// var score = 100;
// var score = 60;
var score = 86;

if(score<60){
    alert('不及格');
}
if(score>=60&&score<80){
    alert('一般');
}
if(score>=80&&score<90){
    alert('良好');
}
if(score>=90&&score<=100){
    alert('优秀');
}

for

for(var i=1;i<=9;i++){
    for(var j=1;j<=i;j++){
       console.log( i+'x'+j+'='+i*j);
    }
}

while

var i=1;
while(i<=9){
    var j=1;
    while(j<=i){
        console.log( i+'x'+j+'='+i*j);
        j++;
   				 }
    i++;
			}

do while

var i=1;
do {
    var j=1;
    do {
        console.log( i+'x'+j+'='+i*j);
        j++;
    } while(j<=i);
    i++;
} while(j<=9);

五.强制类型转换

  • parseInt() 转换为整数   isNaN 判断是NaN

  • //  var num =12;
    //  var num ='12';
    //  var num ='12.122';
     var num ='asdasfs';
    num = parseInt(num);
    if(isNaN(num)){
        console.log('转换失败');
    }
    console.log(typeof(num));
    console.log(num);

六.数组

var arr =[1,2,3,4,5,66,77,88];
console.log(arr);
  • arr.shift 从开始取出一个元素

arr.shift();
console.log(arr);
  • arr.unshift 从开始增加一个元素

arr.unshift('abc');
console.log(arr);
  • arr.push  从后面增加一个元素

arr.push('ABC');
console.log(arr);
  • arr.pop 从后面弹出一个元素

arr.pop();
console.log(arr);
  • arr.splices(取值(可做删除))(index(下标),howmany(数量)

arr.splice(1,3);
console.log(arr);
  • arr.indexOf(searchvalue(找谁),formindex(从哪里开始找)返回的是元素真实的位置),用于关键字寻找

var res=arr.indexOf(66,6);
alert(res);
if(res ==-1){
    alert('是两位数字')
}
console.log(arr);
  • 遍历

var a=[11,22,33,44];
for(i=0;i<=a.length;i++) {
    console.log(a[i]);
}

var i=0;
while(i<a.length){
    console.log(a[i]);
    i++;
}
  • 点击跳转

location.href='https://www.php.cn';//直接打开
window.open('https://www.php.cn')//新页面跳转(有的浏览器会自动拦截窗口)类似于
 <a href="https://www.php.cn" target="_blank"></a>
  • 鼠标事件

    •       onmouseover="over()"  鼠标滑过事件

    •       onmouseleave="leave()"  鼠标离开事件

<div style="background: aquamarine; width: 20%; height: 20px;" onmouseover="over()" onmouseleave="leave()">
function over(){
    console.log('鼠标滑过了')
    
}
function leave(){
    console.log('鼠标离开了')
}


  •  鼠标失去焦点出发 校验事件          

<input type="text" placeholder="22" onblur="check()" >
function check(){
     alert('非法操作');
        }
  • 鼠标联动事件

<select name="" id="" onchange="change()">
    <option value="1">中国</option>
    <option value="2">外国</option>
</select>
    function change(){
        alert('你改变了国家');
    }





Correction status:qualified

Teacher's comments:理解了事件 也就理解了回调
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments