我一直在运行一些测试,看看我是否能够找到一个有效的选择器控件前缀的Asp.Net父控件的ID任何人都可以击败这个jQuery选择器?
我一直在寻找这个,因为我是可以选择从外部JavaScript文件的Asp控件(我厌倦了使用ClientID)。
为了测试我设置了一个包含150个文本框的页面,所有这些页面都带有cssclass“speedy”和一个单独的id,并运行以下代码来选择第107个文本框。
function testclientInput() {
var iterations = 100;
var totalTime = 0;
// Record the starting time, in UTC milliseconds.
var start = new Date().getTime();
// Repeat the test the specified number of iterations.
for (i = 0; i < iterations; i++) {
// Execute the selector. The result does not need
// to be used or assigned to determine how long
// the selector itself takes to run.
// All tests done in ie6, ie7, ie8, ie9beta, firefox3.6, opera11 & chrome8
// slowest
// $('input.speedy[id$=_TextBox107]');
// Quick but only useful if you know the index.
//$($('input.speedy')[106]);
//$('[id$=_TextBox107]:first');
//$('input[id$=_TextBox107]');
//$.clientID("TextBox107");
//$('[id$=_TextBox107]');
//$('input[id$=_TextBox107]:first');
//$($('[id$=_TextBox107]')[0]);
// Fastest
//$($('input[id$=_TextBox107]')[0]);
}
// Record the ending time, in UTC milliseconds.
var end = new Date().getTime();
// Determine how many milliseconds elapsed
totalTime = (end - start);
// Report the average time taken by one iteration.
alert(totalTime/iterations);
};
这是我想到的最好的。 $($('input[id$=_TextBox107]')[0]);
。结果令人惊讶......我期望使用:first
选择器来匹配我的版本,但它报告的结果要慢得多。
任何人都可以想出一种方法来优化呢?
对不起,为什么不能使用`document.getElementById('_ TextBox107')`? ID无论如何都应该是唯一的 – Harmen 2011-01-06 20:16:08
@Harmen:他使用*属性以*选择符结束,所以`_TextBox107`只是ID的最后部分。 – user113716 2011-01-06 20:17:23
@Harmen:因为asp控件的前缀是他们的父ID。我在我的问题中说明了这一点。 – 2011-01-06 20:19:24