js相关用法

一.location
1.返回当前网址url
location.href
2.设置跳转网址url
location.href = "http://www.baidu.com"
3.重载刷新
location.reload()
二.计时器
1.设置延时setTimeout(执行的函数,延时时间)
setTimeout(function(){alert("hehe")},1000)
2.清除window下的某个延时定时器
clearTimeout(setTimeout(function(){alert("hehe")},1000)的返回值
3.间隔执行,每隔制定间隔时间执行一次
setInterval(function(){alert("hehe")},5000)
4.清楚window下的某个间隔定时器
clearInterval(function(){alert("hehe")},5000)的返回值

二.DOM对象

通过这个方法找到id="a"的标签
getElementByid("a")
通过这个方法找到class="c1"的所有标签,返回一个列表,通过索引取到你想获得的标签
getElementsByClassName("c1")
通过这个方法找到标签
getElementsByTagName("a")

创建a标签
var a1 = createElement("a")
添加标签属性
a1.href = "http://www.baidu.com"
a1.innerText = "百度"
给一个父级标签添加一个子标签
d1.appenchild(a1)
添加到这个父级标签中哪个子标签的前面
d1.insertbefore(a1,a2) ===> a1插到已存在标签实例化的a2对象的前面

三.绑定事件

1)
<div id = "d1" onclick = "f(this)">哈哈</div>
js:
function f(ths){
ths.style.backgroundColor = "orange";
}
2)
<div id = "d1" >哈哈</div>
js:
var dive = document.getElementById("d1")
dive.onclick = function(){
this.style.backgroundColor = "orange";
}

原文地址:https://www.cnblogs.com/duoduoyichen/p/10562643.html