除非我的测试有问题,否则当我在 Chrome 上运行这个 jsfiddle 时,$("#id")
选择器大约需要 11 毫秒,选择器大约需要 56 毫秒$(div#id)
。
$(document).ready(function(){
startTime = new Date().getTime();
for (i = 0; i < 10000; i++)
{
s = $("#idC12");
}
$("#idResults").html("c12 by id only time: "+elapsedMilliseconds(startTime));
startTime = new Date().getTime();
for (i = 0; i < 10000; i++)
{
s = $("div#idC12");
}
$("#classResults").html("c12 by tagname#id: "+elapsedMilliseconds(startTime));
});
function elapsedMilliseconds(startTime)
{
var n = new Date();
var s = n.getTime();
var diff = s - startTime;
return diff;
}