您的位置:首页 > Web前端 > JavaScript

JS基础学习day1

2017-09-03 23:27 253 查看
Document.body是 body 标签对象,

Document.documentElementhtml : 标签对象


document.getElementsByTagName获取的是数组还是对象?

答:是一个伪数组,本质上是一个对象,但有length属性。

ECMAscript就是script中的一些语法

BOM:浏览器对象,全称是 brower Object Model

bom是 网页一打开就会存在的东西,不能修改

DOM:文档对象模型,document object Model

dom是要去操作的代码

document 是连接dom和bom的,document有下级,其他的都没有下级了【多窗口】

window对象最高级

window的方法:

window.open :打开一个新窗口

window.open("","",)

定时器:

①一次性定时器:

window.setTimeout("js 代码",时间t);

执行:在时间t之后执行js代码【只会执行一次】

时间:是以毫秒为单位的。

window.setTimeout('alert("Hello")',3000);

②反复性定时器:

window.setInterval("js 代码",时间t);

执行:每过时间t就会执行一次js代码【执行n次】

时间:是以毫秒为单位的。

window.setInterval('alert(456)',2000);


上边两个步骤都是在创建定时器。下面我们学习清除定时器:

①清除一次性定时器:

window.clearTimeout();

但是定时器没有名字,所以我们要给定时器加一个名字:var dingshiqi = window.setTimeout("js 代码",时间);

window.clearTimeout(dingshiqi);

②清除反复性定时器:

window.clearInterval();

同样这个也需要定时器的名字。

原理同上。

需要注意的点:要想清除定时器,必须给定时器名字!!匿名定时器无法清除!!

写一个时间的定时器小李子:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JS_Study</title>
<style>
div{
border:10px solid blue;
background: yellow;
height:100px;
font-size: 60px;
text-align: center;
}
</style>
</head>
<body >
<div>
<script>
var data =new Date();
document.write(data);
</script>
</div>
<script>
window.onload = init;
function init() {
window.setInterval("fn()",1000);
}
function fn() {
var data =new Date();
var n = document.getElementsByTagName("div");
n[0].innerHTML = data;
}
</script>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  前端 定时器 JS