13、动态样式设置
document.getElementById()获得元素
元素.style.属性 = 值;
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#box{
width: 200px;
height: 100px;
background-color: red;
}
</style>
</head>
<body>
<div id="box" onclick="changeColor()"></div>
</body>
<script type="text/javascript">
/*
* 动态样式设置:
* document.getElementById()获得元素
* 元素.style.属性 = 值;
*/
function changeColor(){
var box = document.getElementById("box");
box.style.backgroundColor = "rgb(0,0,255)";
}
</script>
</html>
14、函数
function 函数名字(参数列表){
return 返回值;
}
注意:
<script>
var fn = new Function("a","b","alert(a+b)");
fn(1,2);//3
var fn2 = function(){alert("abc")}
fn2();//abc
</script>
分类
1.无参数无返回值
function 函数名字(){
}
<script type="text/javascript">
// 无参数无返回值
function show(){
alert('hello');
}
//调用函数
show();
show();
</script>
2.有参数无返回值
function 函数名字(参数列表){
}
<script type="text/javascript">
//有参数无返回值函数
function show(a, b) {
var c = a + b;
alert(c);
}
//调用函数
show(5,3);
//实参和形参不需要一一对应
show(7);//7+undefined NaN
</script>
3.无参数有返回值
function 函数名字(){
return 返回值;
}
<script type="text/javascript">
//无参数有返回值函数
function show(){
return 10;
}
var zhi = show();
alert(zhi);
alert(show());
</script>
4.有参数有返回值
function 函数名字(参数列表){
return 返回值;
}
<script type="text/javascript">
//有参数有返回值函数
function js(a, b) {
var c = a + b;
return c;
// alert("aa");//永远没有机会执行
}
alert(js(5, 4));
</script>
15、作用域
全局变量
- 不用var声明时,如果变量在函数中,它不是局部变量,而是全局变量
- 全局变量还可以用window.变量
<script type="text/javascript">
//全局变量
var a = 1;
window.str = "hello"; //var str = "hello";
function show() {
alert(a);
}
function show2() {
b = 2; //没有var声明,默认就是全局变量
alert(b);
}
function show3() {
alert(b + 1);
alert(str);
}
function show4() {
//局部变量
var d = 10;
alert(d);
}
function show5() {
alert(d + 1); //错误
}
show();
show2();
show3();
show4();
show5();
</script>
16、定时器
- setTimeout(函数, 时间毫秒):延迟多少毫秒,执行一次,只调用1次
- setInterval(函数, 时间毫秒):每隔多少毫秒,执行一次,调用无数次
清除定时器:
- clearTimeout:清除setTimeout,也可以清除setInterval
- clearInterval:清除setInterval,也可以清除setTimeout
setTimeout代码
<script type="text/javascript">
//setTimeout 定时器 第一个参数是函数,第二个参数是延迟多少毫秒
setTimeout(function() {
alert("1");
}, 2000);
// function aa(){
// alert("aa");
// }
// setTimeout("aa()",3000);
</script>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="result" onmouseover="clearNum()" onmouseleave="startNum()">0</div>
</body>
<script type="text/javascript">
var result = document.getElementById("result");
result.style.width = "100px";
result.style.height = "100px";
result.style.backgroundColor = "pink";
result.style.textAlign = "center";
result.style.lineHeight = "100px";
result.style.fontSize = "32px";
var n = 0;
// var myTimeout;
function num(){
result.innerText = n;
n++;
myTimeout = setTimeout("num()",1000);
}
num();
function clearNum(){
/*
* 清除定时器:clearTimeout
*/
clearTimeout(myTimeout);
}
function startNum(){
myTimeout = setTimeout("num()",0);
}
</script>
</html>
setInterval代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#result {
width: 100px;
height: 100px;
background-color: pink;
font-size: 32px;
text-align: center;
line-height: 100px;
}
</style>
</head>
<body>
<div id="result" onmouseover="stopNum()" onmouseleave="startNum()">0</div>
</body>
<script type="text/javascript">
var count = 0;
var result = document.getElementById("result");
function stopNum(){
/*
* 清除定时器:clearInterval
*/
clearInterval(myInterval);
}
function startNum(){
myInterval = setInterval("num()", 1000);
}
function num() {
console.log(count);
result.innerHTML = count;
count++;
}
//每隔多少毫秒执行一次函数
var myInterval = setInterval("num()", 1000);
</script>
</html>
17、隐藏显示图片
<body>
<img id="result" src="img/1.jpeg" width="300" height="200" /><br />
<input type="button" value="显示" onclick="show(true)" />
<input type="button" value="隐藏" onclick="show(false)" />
</body>
<script type="text/javascript">
function show(flag) {
var myImg = document.getElementById("result");
var val = flag == true ? "block" : "none";
myImg.style.display = val;
//var val = flag == true ? "visible" : "hidden";
//myImg.style.visibility = val;
}
</script>
18、类
类:是属性与函数的集合
定义一个类
function 类名(参数列表){
this.属性名 = 值;
...;
this.方法名 = function(参数){
};
...
}
创建对象:var 对象名 = new 类名(参数);
访问属性:对象名.属性名;
访问方法:对象名.方法名(参数);
<script type="text/javascript">
function Boy(h, w, s) {
//属性身高
this.h = h;
//体重
this.w = w;
//性别
this.s = s;
//方法
this.show = function() {
alert("身高:" + this.h + "m,体重:" + this.w + "kg,性别:" + this.s);
}
}
var b1 = new Boy(1.8, 70, "男");
//alert("身高:" + b1.h + "m,体重:" + b1.w + "kg,性别:" + b1.s);
b1.show();
var b2 = new Boy(1.6, 50, "女");
//alert("身高:" + b2.h + "m,体重:" + b2.w + "kg,性别:" + b2.s);
b2.show();
</script>
可以对任何对象增加属性和方法:例如:a.type = "猫" a.show = function(){}
可以对属性和方法进行删除:例如:delete a.type delete a.show
<script type="text/javascript">
function Animal(color) {
this.color = color;
}
var a = new Animal("黄色");
console.log(a);
//增加对象中的属性
a.type = "猫";
console.log(a);
var b = new Animal("绿色");
console.log(b);
//删除对象中的属性
delete b.color;
console.log(b);
//增加对象中的方法
a.show = function(){
alert("增加的方法");
}
console.log(a);
a.show();
//删除方法
delete a.show;
console.log(a);
</script>
所有的类的父类是Object,可以直接定义对象
var e1 = {}; 这个时候定义的对象是Object对象,等价于 var e1 = new Object();
var e1 = {
属性名: 值,
...
方法名: 方法
};
<script type="text/javascript">
var e1 = {
ename: "tom",
esex: "男",
ejob: "程序员",
esalary: 10000,
eat: function(){
alert("吃饭。。。");
}
};
console.log(e1);//Object 没有类名
e1.eat();
alert(e1.ename);
var e2 = {};
e2.color = "黑色";
e2.type = "小狗";
e2.show = function(){
alert("这是一只"+e2.color+"的"+e2.type);
}
console.log(e2);
e2.show();
</script>
19、类,数组
- 类中可以有数组,类
- 数组中可以数组,类
<script type="text/javascript">
// 类中可以有数组,类
var stu = {
stuName: "tom",
setSex: "男",
stuTel: [13896547895, 13578451265],
stuAddress: "火星",
cat: {
color: "橘色",
cname: "大黄"
}
}
console.log(stu);
alert(stu.stuTel[0]);
alert(stu.cat.color);
//数组中有类
var arr = [{
stuNo: 1,
stuName: "tom",
stuScore: 98
}, {
stuNo: 2,
stuName: "amy",
stuScore: 99
}, {
stuNo: 3,
stuName: "jack",
stuScore: 88
}];
console.log(arr);
alert(arr[0].stuName);
</script>