Append onclick method using for loop

前端 未结 1 2052
悲哀的现实
悲哀的现实 2021-01-26 09:51

I\'m appending onclick events to elements that I\'m creating dynamically. I\'m using the code below, this is the important part only.

Test.prototype.Show= functi         


        
1条回答
  •  囚心锁ツ
    2021-01-26 10:17

    It's a classical problem. When the callback is called, the loop is finished so the value of i is content.length.

    Use this for example :

    Test.prototype.Show= function (contents) {
        for (var i = 0; i < contents.length; i++) { // no need to have <= and -1
             (function(i){ // creates a new variable i
               var menulink = document.createElement('a');
               menulink.href = "javascript:;";
               menulink.onclick = function () { return that.ClickContent.apply(that, [contents[i]]); };
             })(i);
        }
    }
    

    This immediately called function creates a scope for a new variable i, whose value is thus protected.

    Better still, separate the code making the handler into a function, both for clarity and to avoid creating and throwing away builder functions unnecessarily:

    Test.prototype.Show = function (contents) {
        for (var i = 0; i <= contents.length - 1; i++) {
            var menulink = document.createElement('a');
            menulink.href = "javascript:;";
            menulink.onclick = makeHandler(i);
        }
    
        function makeHandler(index) {
            return function () {
                return that.ClickContent.apply(that, [contents[index]]);
            };
        }
    };
    

    A way to avoid this problem altogether, if you don't need compatibility with IE8, is to introduce a scope with forEach, instead of using a for loop:

    Test.prototype.Show = function (contents) {
      contents.forEach(function(content) {
        var menulink = document.createElement('a');
        menulink.href = "javascript:;";
        menulink.onclick = function() {
          return that.ClickContent.call(that, content);
        };
      });
    }
    

    0 讨论(0)
提交回复
热议问题