Parameters of function in for Loop with let

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload=function(){
  var lis = document.getElementsByTagName("li");
  var odiv = document.getElementById("antzone");
  for (let index = 0; index < lis.length; indexPP) {
    lis[index].onclick = function () {
      odiv.innerHTML = index;
    }
  }
}
</script>
</head>
<body>
<div id="antzone"></div>
<ul>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
</ul>
</body>
</html>

ask why the parameter index:
lis.onclick = function (index) {

cannot be written in the function after onclick.
 odiv.innerHTML = index;

}

Feb.17,2022

because the event you bind will have a callback, and the parameter at this time is the value of the callback! For example, there is a callback in index , which contains the parameters when the event is triggered. In ie , index is global and can be obtained through window.event , which is passed as a parameter in other browsers.

if you want to pass parameters, there are several ways:

the first type
for (let index = 0; index < lis.length; indexPP) {
  lis[index].onclick = test(index,odiv);
}
function test(index,odiv){
  console.log(index);
  return function(){
      odiv.innerHTML = index;
  }
}
the second type
 (lis[index].onclick=function(){
    console.log(index);
    odiv.innerHTML = index;
 })(index);
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e4fa05-595d2.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e4fa05-595d2.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?