outerHtml/innerHTML/innerHTML
自定义属性及getAttribute/setAttribute方法
<div id="d"></div>
var d = document.getElementById("d");
d.setAttribute("heihei",123);
document.write(d.getAttribute("heihei"));
获取非行内样式(兼容问题)
- 行内样式:行内样式就是卸载标签内的样式:如
<div style="color:red;"></div>
,在这里color样式就是行内样式. - 非行内样式:非行内样式就是指不是写在标签体内的style属性的样式.如
<style>...</style>
内的定义样式或引用的外部css文件都是非行内样式.
<div id="box" style="color: red;"> </div>
var d = document.getElementById("box");
document.write(d.style.color);
#box{
color: blue;
}
var d = document.getElementById("box");
document.write(d.style.color); 返回空
----------------------------------------
#box{
color: red;
font-size: 22px;
}
var d = document.getElementById("box");
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj,false)[attr];
};
};
document.write(getStyle(d,"color") + "</br>");
document.write(getStyle(d,"font-size"));
insertBefore:添加到一个节点前面
- 父节点.insertBefore(要添加的子节点,参照节点);参照节点如果为null那就和appendChild()一样
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<ul id="list">
<li>槑槑</li>
<li>晶晶</li>
<li>叕叕</li>
</ul>
</body>
</html>
<script>
var list = document.getElementById("list");
var o = document.createElement("li");
o.innerHTML = "淼淼";
list.insertBefore(o,list.children[2]);
</script>
offsetWidth/offsetHeight/offsetLeft/offsetTop
<style>
*{
margin: 0;
padding: 0;
}
#big{
border: 1px solid red;
position: relative;
height: 400px;
width: 400px;
}
#small{
border: 1px solid red;
position: absolute;
height: 100px;
width: 100px;
left: 80px;
top: 70px;
}
</style>
</head>
<body>
<div id="big">
<div id="small">
</div>
</div>
</body>
</html>
<script>
var d = document.getElementById("small");
document.write(d.offsetLeft + " " + d.offsetTop + "</br>");
document.write(d.offsetWidth + " " + d.offsetHeight);
style.left返回字符串 如20px, offsetLeft返回数值 如20
style.left读写,offsetLeft只读
style.left必须要事先定义,否则值为空
window.onscroll事件
回到顶部的案例
<body>
<div id="t">
<p>111111111111111</p>
....
<p>111111111111111</p>
</div>
<button onclick="fun()">按钮</button>
</body>
</html>
<script>
var _top = null;
window.onscroll = function(){
_top = document.body.scrollTop || document.documentElement.scrollTop;
console.log(_top);
}
function fun(){
document.body.scrollTop = document.documentElement.scrollTop = 0;
}
</script>
事件
- 事件对象
- 事件:对某个元素的某种操作
- 事件对象:当某个事件触发时产生的对象,就是事件对象.
- event使用前提,必须有事件,不同的事件产生的事件对象不同.
- 事件对象的兼容性:var e = evt || event;
- 事件对象拥有该事件先关的属性和方法
document.onkeydown=function(evt){
var e = evt || event;
alert(e);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#box {
height: 100px;
width: 100px;
background: url(img/1.png);
background-size: 100px 100px;
position: absolute;
left: 200px;
top: 600px;
}
</style>
</head>
<body>
<div id="box" onclick="jump()"></div>
</body>
</html>
<script>
var d = document.getElementById("box");
function jump() {
d.style.top = d.offsetTop - 200 + "px";
setTimeout(function() {
d.style.top = "600px";
}, 2000);
}
</script>
- 鼠标事件对象的属性
- 坐标属性:
pageX/pageY相对于整个文档顶部和左侧的坐标
clientX/clientY相对于局部窗口的左侧和顶部的坐标
offsetX/offsetY 相对于内部元素 的距离左侧和顶部的坐标 常用于拖拽 document.onclick = function(evt) {
var e = evt || event;
console.log("client" + e.clientX + "," + e.clientY);
console.log("page" + e.pageX + "," + e.pageY);
console.log("offset" + e.offsetX + "," + e.offsetY);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
#tiger{
height: 100px;
width: 100px;
background-image: url(img/3.jpg);
background-size: 100px 100px;
position: absolute;
left: 200px;
top:500px;
}
</style>
</head>
<body>
<div id="tiger">
</div>
</body>
</html>
<script>
var oTiger = document.querySelector("#tiger");
document.onmousemove = function(evt){
var e = evt || event;
oTiger.style.left = e.pageX - oTiger.offsetWidth/2 + "px";
oTiger.style.top = e.pageY - oTiger.offsetHeight/2 + "px";
}
</script>
- 键盘事件