我正在用法语做一个类似Reddit的网站,并且要完全优化我正在获取结果,缓存它,然后通过jQuery查询每个链接,看看它们是否被downvoted/upvoted.
您如何看待优化查询?
为什么不起作用!这是我的代码.
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>
Run Code Online (Sandbox Code Playgroud)
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);
});
});
Run Code Online (Sandbox Code Playgroud)
我有这样的多个盒子,它只会影响最后一个盒子.我首先尝试没有索引和eq(索引),它做了同样的事情.
您覆盖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);
});
});
Run Code Online (Sandbox Code Playgroud)
这样做的另一个改进是使用.each()正在执行回调的上下文.这会稍微加快你的脚本,因为选择器不需要再次评估,你只需将DOM元素(this)包含在jQuery对象中:
$(".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);
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2118 次 |
| 最近记录: |