首页 > 代码库 > js - 02课 4 浅谈this -2

js - 02课 4 浅谈this -2

1. 防止this重名,可以通过设置值,然后通过参数传递过去

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<input type="button" value="http://www.mamicode.com/按钮1"/>
<input type="button" value="http://www.mamicode.com/按钮2"/>
<input type="button" value="http://www.mamicode.com/按钮3"/>
<script>
    window.onload = function(){
        var aBtn = document.getElementsByTagName(‘input‘);
        for (var i = 0; i < aBtn.length; i++) {
            aBtn[i].onclick = function(){
              var _this = this;
              changeBackground(_this);//前面不带东西 就是 : window.changeBackground(_this)
            }
        }
    }
    function changeBackground(_this){
        _this.style.background = ‘red‘;
    }
</script>
</body>
</html>

  

js - 02课 4 浅谈this -2