开发者

Speed up :visible:input selector avoiding filter

开发者 https://www.devze.com 2022-12-23 06:11 出处:网络
I have a jQuery selector that is running way too slow on my unfortunately large page: $(\"#section\").find(\":visible:input\").filter(\":first\").focus();

I have a jQuery selector that is running way too slow on my unfortunately large page:

$("#section").find(":visible:input").filter(":first").focus();

Is there a quicker way to select the first visible input without having to find ALL the visible inputs and then filtering THAT selection for the first? I want something like :visible:input:first but that d开发者_StackOverflow社区oesn't seem to work.

[Edit] Here's the basic idea of what #section looks like:

<div id="section">
    <div>
        Some text <input type="text">
    </div>
    <div>
        etc. etc. <input type="text">
    </div>
</div>


$(":input:visible:first", "#section").focus();

If you first filter for the type of control you avoid checking the :visible on all the #section's elements.

It seems like you need only to catch the first input type="text" visible.
This should be a bit faster.

$("input[type='text']:visible:first", "#section").focus();


How about adding class="default_field" to the default field for each page, then using $('.default_field').focus();?

How easy this is to do depends on your server-side technology of course, but the advantages are that it takes the processing burden off of the client (which is extra important for IE6), and it also gives you the flexibility to choose a default input other than the very first one on pages where it's appropriate.

0

精彩评论

暂无评论...
验证码 换一张
取 消