0
var pageInit = (function(){
    var regBtn = function(){
        var ls = document.querySelectorAll("li");
        for (var i = 0; i < ls.length; i++) {
            ls[i].onclick = menuClick;  
        }
    };
    var menuClick = function(){
        console.log(ls[i]);
    };
    return {
        regBtn: regBtn
    };
})();

window.onload = function(){
    pageInit.regBtn();
};

I'm starting modulation of coding javascript now.
But faced with the problem of function scope.
I know what the problem is but don't know how to solve it.
menuClick can't read the ls[i].
I tried ls[i].onclick = this.menuClick; or menuClick.bind(this) but useless.
How to solve problems like this?
I know how closure is but dont know how to solve this through bind() call() etc.

yaochiqkl
  • 489
  • 3
  • 13

0 Answers0