JS 基础函数

1.弹出的输入框

    var score  = window.prompt("请输入你的成绩"); 
		if(score > 60){
			document.write("congratulation!");
		} else{
			document.write("你也太菜了吧");
		}
		document.write("<br>");

2.提取变量中刚开始的数字

var px = "100px";
	document.write(parseInt(px)+"<br>"); // 提取整数
	document.write(parseFloat(px)+"<br>");// 提取小数
    //如果变量中的第一个不是数字,就会得到NaN,遇到非数字停止

3.判断变量是否为数字

var px = "1541.02afdkj";
document.write(isNaN(px)+"<br>") // px为数字,false

4. for循环

var arr = ["wo", "ni", "ta", 15];
for(var i in arr){
	document.write(arr[i] + " ");   i为数组下标,arr为数组名
}

5.alert()为提示框,一般用于调试

6.函数的调用 不需先后顺序,同名函数 会将先前的覆盖    , 形参如果有默认值,放在形参列表的最后面

7.

 //1.将匿名函数赋值给变量
	var a = function(a){
	    return "abcd" + a;
	}
	var res = a("hello");
	document.write(res);

	//2.将匿名函数赋值给事件
	// ****** 重点 *******
	// 设置网页背景颜色
	// 将匿名函数赋值给一个事件  onload 加载后 事件它需要有一个对象来调用
	// 事件三要素 事件源 事件名  动作
	window.onload = function(){
	    document.body.style.backgroundColor = "#f00";
	}

	// 3. 匿名函数的自调用
	(function(){
		document.write("hello");
	})();

猜你喜欢

转载自blog.csdn.net/error311/article/details/85388538