This
this
this的作用
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
<title>Document</title>
<style>
div {
width: 100px;
height: 100px;
background-color: green;
margin: 10px;
}
</style>
</head>
<body>
<script>
window.onload = function () {
var myDiv = document.getElementsByTagName('div');
for (var i = 0; i < myDiv.length; i++) {
myDiv[i].onclick = function () {
console.log(i);
console.log(this.id);
}
}
}
</script>
<section>
<div id="div0"> div0</div>
<div id="div1"> div1</div>
<div id="div2"> div2</div>
<div id="div3"> div3</div>
<div id="div4"> div4</div>
</section>
</body>
</html>全局作用域中的this
this的定律
函数赋值给变量时,this指向window
以函数形式调用时,this永远都是window
以方法的形式调用时,this是调用方法的对象
解决闭包中的this指向问题
当this遇到一些特殊的函数时
Last updated