Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
哪种jQuery方法可以更好地自动调整父DIV宽度上的子对象?_Jquery_Html_Performance_Fluid Layout - Fatal编程技术网

哪种jQuery方法可以更好地自动调整父DIV宽度上的子对象?

哪种jQuery方法可以更好地自动调整父DIV宽度上的子对象?,jquery,html,performance,fluid-layout,Jquery,Html,Performance,Fluid Layout,根据对我的另一个问题(这个:)的回答,我想哪种jQuery方法是解决性能问题的最佳方法 块1:在需要时查找所有DOM元素: 块2:仅查找DOM子项,使用each()、parent()和sibbines(): 块3:首先查找DOM父对象,使用each()并根据上下文查找子对象: 如果有人想测试,这里有小提琴: 那么,哪一块更好?为什么呢?我认为块1最好,因为它不需要循环 您也可以尝试以下方法: $("div.parent a").css({ width: $(this).parent().

根据对我的另一个问题(这个:)的回答,我想哪种jQuery方法是解决性能问题的最佳方法

块1:在需要时查找所有DOM元素:

块2:仅查找DOM子项,使用each()、parent()和sibbines():

块3:首先查找DOM父对象,使用each()并根据上下文查找子对象:

如果有人想测试,这里有小提琴:


那么,哪一块更好?为什么呢?

我认为
块1
最好,因为它不需要循环

您也可以尝试以下方法:

$("div.parent a").css({
    width: $(this).parent().width() / $(this).siblings().length - 2)
});

我将预先查询元素,如下所示:

// find the elements you want to resize
var $links = $("div.parent a");

// resize them, and use the parent width
$links.width($links.parent().width() / $links.length - 2);
通过这种方式,您可以查找链接,并且只查找一次家长。没有循环,也没有多余的查询

下面是一个例子:

使用:

  • 第1区:8.466毫秒,372次呼叫
  • 第2区:10.130毫秒,526次呼叫
  • 第三组:8.560毫秒,383次呼叫
西克索尼亚的回答也是最快的

  • 希克索尼亚:8.205毫秒,369次呼叫
按速度顺序:

  • 西克索尼亚氏
  • 第一区
  • 第3区

  • 甚至不使用block 2

    “css({…})”不会改变“this”的范围,因此$(this)将引用包含本地“this”的jQuery对象,而不是单个链接。我认为jQuery确实可以改变
    css()
    和许多其他方法的
    this
    的范围。但这是对的:不起作用。很好!我用你的方法更新了我的小提琴。要简单得多。谢谢
    $("div.parent").each(function() {
        $("a", this).css("width", $(this).width() / $("a", this).length - 2);
    });
    
    $("div.parent a").css({
        width: $(this).parent().width() / $(this).siblings().length - 2)
    });
    
    // find the elements you want to resize
    var $links = $("div.parent a");
    
    // resize them, and use the parent width
    $links.width($links.parent().width() / $links.length - 2);