2015-11-17 34 views
0
var imgs = ['bmw.jpg', 'bugatti.jpg', 'classic.jpg', 'concept.jpg', 'corvette.jpg', 'dino.jpg', 'lambo.jpg', 'mcclaren.jpg', 'p1.jpg', 'porsche.jpg', 'rally.jpg', 'audi.jpg']; 
     // var imgs_count = {'bmw.jpg': 0, 'bugatti.jpg': 0, 'classic.jpg': 0, 'concept.jpg': 0, 'corvette.jpg': 0, 'dino.jpg': 0, 'lambo.jpg': 0, 'mcclaren.jpg': 0, 'p1.jpg': 0, 'porsche.jpg': 0,'rally.jpg': 0, 'audi.jpg':0} 

     // for (var i in imgs_count) { 
     // imgs_count[i] 
     // } 

     var allCars = []; 
     var votes; 

     function Car(file) { 
      this.file = file; 
      this.votes = 0; 
      allCars.push(this); 
     } 

     var bmw = new Car('bmw.jpg'); 
     var bugatti = new Car('bugatti.jpg'); 
     var classic = new Car('classic.jpg'); 
     var concept = new Car('concept.jpg'); 
     var corvette = new Car('corvette.jpg'); 
     var dino = new Car('dino.jpg'); 
     var lambo = new Car('lambo.jpg'); 
     var mcclaren = new Car('mcclaren.jpg'); 
     var p1 = new Car('p1.jpg'); 
     var porsche =new Car('porsche.jpg'); 
     var rally = new Car('rally.jpg'); 
     var audi = new Car('audi.jpg'); 

     var idx1 = 0; 
     var idx2 = 0; 

     var path = 'cars/'; 
     var done = false; 

     function getRandomImage() { 
      idx1 = Math.floor(Math.random()*imgs.length); 
      var img1 = imgs[idx1]; 
      idx2 = idx1; 
      while (idx2 == idx1) { 
      idx2 = Math.floor(Math.random()*imgs.length); 
      } 
      img2 = imgs[idx2]; 
      document.getElementById('choice1').setAttribute('src', path + img1); 
      document.getElementById('choice2').setAttribute('src', path + img2); 

     } 

     function setOnClicks(id) { 
      document.getElementById(id).addEventListener('click', function(event) { 
      var choice = event.target.id; 
      if (choice === 'choice1') { 
       console.log(allCars); 
       var img = document.getElementById(id).getAttribute('src'); 
       var allCars = img.slice(5,img.length); 
       this.allCars[idx1].votes += 1; 
       console.log(idx1); 
       console.log(this.allCars[idx1].votes) 
       if (allCars[idx1].votes=3) { 

        done = true; 

       } 
      } 
      if (choice === 'choice2') { 
       var img = document.getElementById(id).getAttribute('src'); 
       var allCars = img.slice(5,img.length); 
       console.log(idx2); 
       console.log(this.allCars[idx2].votes); 
       this.allCars[idx2].votes += 1; 

       if (allCars[idx2].votes == 3) { 
       done = true; 
       } 
      } 
      if (!done) 
       getRandomImage(); 
      }); 
     } 



     getRandomImage(); 
     setOnClicks('choice1'); 
     setOnClicks('choice2')l 
+0

我不能得到數組的投票,點擊圖片時添加+1。我得到'Uncaught TypeError'的回報:無法讀取'undefined'屬性'11'或類似的東西,就像在我的'allCars'數組中找不到' –

+0

問題(或至少是_a_問題)是'var allCars'和'this.allCars' _不是一回事。看到這裏的例子:http://jsfiddle.net/866yocrv/ – aroth

+0

如果我刪除(這)它仍然顯示相同的問題 –

回答

0

你跟你的覆蓋全球allCars一個本地的var聲明同名:

​​
+0

錯誤非常感謝!得到它了 –

0

要創建下click事件的局部變量allCars,而this.allCars被點到allCars變量下window對象

var allCars = img.slice(5,img.length); // this is a local variable 
this.allCars[idx1].votes += 1; // this is same as window.allCars 
console.log(idx1);