基于对另一个我的问题的回答(这个:How to make children auto fit parent's width only with CSS?),我在想哪个是解决性能问题的最佳 jQuery 方法。
块 1:在需要时查找所有 DOM 元素:
$("div.parent a").css("width", $("div.parent").width() / $("div.parent a").length - 2);
块 2:仅查找 DOM 子级,使用 each()、parent() 和兄弟姐妹():
$("div.parent a").each(function() {
$(this).css("width", $(this).parent().width() / $(this).siblings().length - 2);
});
块 3:首先找到 DOM 父级,使用 each() 并根据上下文找到子级:
$("div.parent").each(function() {
$("a", this).css("width", $(this).width() / $("a", this).length - 2);
});
如果有人想测试,这里是小提琴:http: //jsfiddle.net/ErickPetru/6nSEj/3/
那么,哪个区块更好呢?为什么?