如何优化和减少该函数中对 jQuery 的调用?
我有一个似乎不是最佳的代码。我想将 Reorder 函数的内容减少为单个 jquery 函数。它可以做得更简单,或者也许这是一个好方法?
HTML:
<div id="sortable">
<div class="i">
@<input type="text" name="first" value="" />
</div>
<div class="i">
@<input type="text" name="last" value="" />
</div>
</div>
<a href="#" id="add_input">Add</a>
JS:
$(function(){
$("#sortable").sortable({
containment: "document",
axis: "y",
update: Reorder,
});
function Reorder()
{
$("#sortable input").attr("name", function(i){
//if(i==0){return "first";}
//else{return "waypoint" + (i + 1); }
return "middle" + i;
});
$("#sortable input:first").attr("name", "first");
$("#sortable input:last").attr("name", "last");
}
$("#add_input").click(function () {
var inputIndex = $("#sortable > .i").length;
$("#sortable input:last").attr("name", function(){
return "middle" + (inputIndex - 1);
});
if(inputIndex>1){
var html = '<div class="i">';
html += '@<input type="text" name="last" value="" /> ';
}
$("#sortable").append(html);
return false;
});
});
演示: jsfiddle
I have a code that seems to be not optimal. I would like to reduce content of Reorder function to single jquery function. It can be done simpler or maybe this is a good approach?
HTML:
<div id="sortable">
<div class="i">
@<input type="text" name="first" value="" />
</div>
<div class="i">
@<input type="text" name="last" value="" />
</div>
</div>
<a href="#" id="add_input">Add</a>
JS:
$(function(){
$("#sortable").sortable({
containment: "document",
axis: "y",
update: Reorder,
});
function Reorder()
{
$("#sortable input").attr("name", function(i){
//if(i==0){return "first";}
//else{return "waypoint" + (i + 1); }
return "middle" + i;
});
$("#sortable input:first").attr("name", "first");
$("#sortable input:last").attr("name", "last");
}
$("#add_input").click(function () {
var inputIndex = $("#sortable > .i").length;
$("#sortable input:last").attr("name", function(){
return "middle" + (inputIndex - 1);
});
if(inputIndex>1){
var html = '<div class="i">';
html += '@<input type="text" name="last" value="" /> ';
}
$("#sortable").append(html);
return false;
});
});
DEMO: jsfiddle
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个稍微优化的版本:
缓存您的 jquery 对象以避免一直重新查询相同的事情:
$('#sortable')
无需使用
.attr(string, function)
只需连接一个字符串,使用.attr('name', 'middle' + (inputIndex-1))
jquery 是关于链接的,滥用它: $inputs.filter().attr().end().filter().attr()
我制作了这个 fiddle 来说明。
事件更优化:o)
您也可以这样编写 Reorder() 函数。循环然后再次过滤似乎有点“愚蠢”。在循环中执行所有操作:
Here is a little bit optimized version:
Cache your jquery objects to avoid re-querying all the time the same thing:
$('#sortable')
No need to use
.attr(string, function)
to simply concatenate a string, use.attr('name', 'middle' + (inputIndex-1))
jquery is about chaining, abuse it: $inputs.filter().attr().end().filter().attr()
I've made this fiddle to illustrate.
Event more optimized :o)
You could also write your Reorder() function this way. Seems a bit 'stupid' to loop and then re-filter again. Do everything in the loop:
您现有的重新排序功能没有太大问题。您可以像这样稍微改进它:
这将
#sortable input
的查找次数从 3 次减少到 1 次。您也可以省略变量声明并将其全部串在一起,如下所示:
就我个人而言,我发现这比前面的示例更难阅读。
There is not much wrong with your existing Reorder function. You could very slightly improve it like so:
That reduces the number of look-ups of
#sortable input
from three to one.You could also leave out the variable declaration and just string it all together, like so:
Personally, I find that a bit harder to read than the previous example.