var allCheckBox=$(".assignmentHouseContent img[src='./resource/images/public/IsNSelect.png']")for(var i =0; i < allCheckBox.length; i++){
console.log(allCheckBox[i]);
allCheckBox[i].addEventListener("click",function(){if(allCheckBox[i].src.indexOf("IsNSelect")!=-1){
allCheckBox[i].src="./resource/images/public/IsSelect.png";
console.log("a")}elseif(allCheckBox[i].src.indexOf("IsSelect")!=-1){
allCheckBox[i].src="./resource/images/public/IsNSelect.png";
console.log("b");}})}
问题解决:绑定事件放在及时运行函数中
var allCheckBox=$(".assignmentHouseContent img[src='./resource/images/public/IsNSelect.png']")for(var i =0; i < allCheckBox.length; i++){
console.log(allCheckBox[i]);//立即运行函数(function(i){
allCheckBox[i].addEventListener("click",function(){if(allCheckBox[i].src.indexOf("IsNSelect")!=-1){
allCheckBox[i].src="./resource/images/public/IsSelect.png";
console.log("a")}elseif(allCheckBox[i].src.indexOf("IsSelect")!=-1){
allCheckBox[i].src="./resource/images/public/IsNSelect.png";
console.log("b");}})})(i)}