首页 > 代码库 > javascript this关键字
javascript this关键字
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
<script type ="text/javascript" >
//this是表示当前监听事件这个对象,event.srcElement是引发事件的对象
function btnClick() {//相应事件函数中不能用this来获取事件对象
alert(this.value);
}
function btnClick2(btn) {//将this作为参数传过来
alert(btn.value);
}
</script>
</head>
<body>
<input type ="button" value ="http://www.mamicode.com/click1" onclick ="alert(event.srcElement.value)" />
<input type ="button" value ="http://www.mamicode.com/click2" onclick ="alert(this.value)" />
<input type ="button" value ="http://www.mamicode.com/click3" onclick ="btnClick()" />
<input type ="button" value ="http://www.mamicode.com/click4" onclick ="btnClick2(this)" />
</body>
</html>