2016-12-15 30 views
1

我試圖實施一個研究欄來學習流星,JavaScript等......問題是我想去第一篇文章與搜索相匹配,但說「有X的文章多用你的標準」如何在第一次迭代中設置一個值,然後永遠不要在Javascript中更改它

我已經試過這樣:

'click .tfbutton'(event){ 
     var research = document.getElementById('tftextinput').value.toLowerCase(); 
     var nombreValide = 0; 
     var lastArticlePos = 0; 

     setTimeout(function(){ 
     $('.titreArticle').each(function(i, obj) { 
      var acutal = obj.textContent.toLowerCase(); 
      if(acutal.includes(research)){ 
       nombreValide = nombreValide + 1; 
       var position = obj.offsetTop; 
       window.scrollTo(0,position); 
       document.getElementById('tftextinput').value = ""; 
      }else{ 
       document.getElementById('tftextinput').value = ""; 
       console.log("We can't find an article with your criterias"); 
      } 
     }); 
     console.log("There is : " + nombreValide + " articles who matchs"); 
     }, 20) 
     }, 

而且我想在第一次迭代設置lastArticlePos然後再也沒有改變,但仍增加1至nombreValide

回答

2

只是null初始化它,並把它設置爲別的東西只有null

'click .tfbutton'(event){ 
     var research = document.getElementById('tftextinput').value.toLowerCase(); 
     var nombreValide = 0; 
     var lastArticlePos = null; 

     setTimeout(function(){ 
     $('.titreArticle').each(function(i, obj) { 
      var acutal = obj.textContent.toLowerCase(); 
      if(acutal.includes(research)){ 
       if (lastArticlePos === null) lastArticlePos = i; 
       nombreValide = nombreValide + 1; 
       var position = obj.offsetTop; 
       window.scrollTo(0,position); 
       document.getElementById('tftextinput').value = ""; 
      }else{ 
       document.getElementById('tftextinput').value = ""; 
       console.log("We can't find an article with your criterias"); 
      } 
     }); 
     console.log("There is : " + nombreValide + " articles who matchs"); 
     console.log("Article position is : " + (lastArticlePos === null) ? "<No article found>" : lastArticlePos); 
     }, 20) 
     }, 
+0

它的工作!謝謝我應該考慮這個解決方案,但我沒有^^'再次謝謝你! – Jerome

相關問題