2012-04-25 132 views
4

我正在做一個類似Reddit的法語網站,爲了充分優化我提取結果,緩存它,然後通過jQuery查詢每個鏈接,看看它們是否被downvoted/upvoted。

  1. 您認爲如何優化查詢?

  2. 它爲什麼不起作用!這是我的代碼。

HTML:

<div class="box ajax_div" title="3"> 
     <div class="score"> 
      <a href="#" class="upvote_position" title="post-up-3"><img src="images/up.png" /></a> 
      <div class="score_position">1</div> 
      <a href="#" class="downvote_position" title="post-down-3"><img src="images/down.png" /></a> 
    </div> 

    <div class="box_info"> 
     <div class="link"> 
      <a href="#" class="text-show"><a href="?show=3" rel="nofollow" class="out">ouioui</a> 
     </div> 
     <div class="further"> 
      <span class="date" title="2012-04-25 04:57:05">il y a 13 heures</span> | posté par <a href="?user=david">david</a> dans <a href="?chan=100hp.fr">100hp.fr</a> 
     </div> 
     <div class="further_more"> 
      <a href="?show=3"><img src="images/comment.png" />2 commentaires</a> <a href="#" class="save" title="3"><img src="images/save.png" />sauvegarder le lien</a> <a href="#" class="spam" title="3"><img src="images/report.png" />spam?</a> 
     </div> 
    </div> 
    <div class="textbox" style="display:none;">razraz</div> 
    </div> 

的JavaScript:

$(".box").each(function(index){ 
     ele = $('.box').eq(index); 
     $.get("ajax/score.php", 
     { idbox: ele.attr('title'), type: "post" }, 
     function(data) { 
      ele.find(".score_position").html(data); 
     }); 
    }); 

我有多個盒子一樣的是,它不僅影響到他們的最後一個。我第一次嘗試沒有索引和eq(索引),它做同樣的事情。

回答

6

您覆蓋ele全局變量,嘗試添加var

$(".box").each(function(index){ 
    var ele = $('.box').eq(index); 
    $.get("ajax/score.php", 
    { idbox: ele.attr('title'), type: "post" }, 
    function(data) { 
     ele.find(".score_position").html(data); 
    }); 
}); 

在這樣做的另一個改進是使用在正在執行的.each()回調的背景下。這將加速你的腳本一點,因爲選擇並不需要進行再次評估,你根本jQuery對象中封裝的DOM元素(this):

$(".box").each(function(index){ 
    var ele = $(this); 
    $.get("ajax/score.php", 
    { idbox: ele.attr('title'), type: "post" }, 
    function(data) { 
     ele.find(".score_position").html(data); 
    }); 
}); 
+0

所以這是隻是一個js問題...我現在感到很蠢,非常感謝! – 2012-04-25 16:24:30

+0

@David天宇黃:不需要感到愚蠢。省略'var'是很常見的錯誤。 – Tadeck 2012-04-25 16:30:20

+0

@David天宇黃:我增加了改進。您可以使用'$(this)'並檢索相同的對象,而不是再次選擇'ele'。 – Tadeck 2012-04-25 16:33:45

1

jQuery的每個功能提供了在「每個」

兩個參數

$('...')。each(function(index,value){...});

只需使用第二個參數。

+0

或使用'$(this)' – Umair 2012-04-25 16:24:51

2

.get請求是異步的,這意味着一旦調用它就不會阻止代碼執行。

實際上,ele不是您認爲最終調用.get回調時的元素(它可能是.box的最後一個元素)。

爲了克服這個問題,請嘗試定位沒有變量的元素,該變量在循環的每次迭代中都會更改。

0

我有同樣的問題。 @Blender給了我最初的想法:謝謝。

我的解決方案是使用:

$.ajax({ 
url: "myExample.html", 
context: this //"this" refer to the outer-context "this" & not the internal $ajax "this" 
}).done(function() { 
//do all the stuff you need to do, it will then be executed in the right order 
}); 

對我來說,如果我不這樣做的,我的代碼的其餘部分完成所以比返回$就調用更快了。如果我使用全局變量,這並不重要。

UPDATE:

當使用$阿賈克斯,只有2個插槽可以從瀏覽器中運行的要求。如果你有很多請求(超過500個),這將導致瀏覽器(我使用Chrome)掛起,因爲請求加起來,但無法按時處理。一段時間後,您將從服務器獲得連接錯誤。

解決方案:使用AJAX隊列管理器。我用http://plugins.jquery.com/ajaxQueue/

所以現在我的代碼略有不同(只與jQuery.ajaxQueue更換$阿賈克斯):

jQuery.ajaxQueue({ 
url: "myExample.html", 
context: this //"this" refer to the outer-context "this" & not the internal $ajax "this" 
}).done(function() { 
//do all the stuff you need to do, it will then be executed in the right order 
}); 

希望這將有助於後代:-)