li集合绑定点击事件

2018-01-12 11:53:17来源:cnblogs.com作者:joker_xd人点击

分享

//Method-1:
var items = document.getElementsByTagName('li');
  for(var i=0;i<items.length;i++){
    items[i].index = i;
    items[i].onclick = function(){
      this.innerHTML = this.index;
  }
}


//Method-2:
var items = document.getElementsByTagName('li');
  for(var i = 0; i<items.length; i++){
    (function(index){
      items[i].onclick = function(){
      this.innerHTML = index;
    }
  })(i)
}


//Method-3:
var items = document.getElementsByTagName('li');
for(var i = 0; i<items.length; i++){
  items[i].onclick = function(index){
    return function(){
      this.innerHTML = index;
    }
  }(i)
}

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台