jQuery 运行 如果HTML 元素名称加上ID 会更快吗?

Will jQuery run faster if the HTML element name is added with the ID?

下面是我在单击 div 时将数据添加到 table 的代码片段:

$(document).on("click", "div#pos_product_id_<?php echo $row['id']; ?>", function() {
    $("table#pos_till_products_list").each(function() {
        searchRow = $(this).find("tr#product_id_<?php echo $row['id']; ?>").html();
        if (searchRow == null) {
            $("#pos_till_products_list tbody:first").prepend("<?php echo $product_build; ?>");
        } else {
            var qty = $(this).find("tr#product_id_<?php echo $row['id']; ?> .qty").html();
            qty++;
            $("tr#product_id_<?php echo $row['id']; ?> .qty").html(qty);
        }
    });
});

我刚开始使用 JS/jQuery,但如您所见,我在 ID 前面添加了 HTML 元素。我想知道如果通过添加 HTML 元素,它会使 jQuery 执行得更快吗?我认为它会,因为添加元素名称会阻止 jQuery 搜索页面上的所有其他 HTML 元素 ID,并且只将您要求它的 HTML 元素定位到 select。

实际上,不同框架处理选择器的方式确实会影响速度,但影响通常很小。查看 SlickSpeed 以查看大量选择器测试的基准比较,从 $("body") 到 $("div:nth-child(n)") 在许多不同的框架上。大多数情况下,差异只是一两毫秒,但这是一个有趣的思想实验。