0

我需要将焦点放在“.focus”的第一个表单元素或类上;无论是可见的还是先出现的。

这似乎没有对每个进行排序以确定哪个先出现?

http://jsfiddle.net/infatti/tdvHJ/1/

$('.focus:visible:first, body:not(:has(.focus:visible)) input:visible:first, body:not(:has(.focus:visible)) textarea:visible:first').focus();
4

4 回答 4

2

This will locate all visible input elements, textarea elements and .focus elements:

$('input:visible, .focus:visible, textarea:visible')

It will also have them ordered according to their order in the DOM, so the first of those elements in the document will be the first in the jQuery object. To access the first:

$('input:visible, .focus:visible, textarea:visible').eq(0);

and to focus on it:

$('input:visible, .focus:visible, textarea:visible').eq(0).focus();

Note that, as I just found out, jQuery considers elements to be 'visible' if they take up space in the document. So elements with visibility:hidden or opacity:0 will still be considered visible:

http://api.jquery.com/visible-selector/

于 2013-09-24T15:07:14.067 回答
0

像这样 ?

$('.focus:visible:first, body:not(:has(.focus:visible)) input:visible:first, body:not(:has(.focus:visible)) textarea:visible:first').eq(0).focus();

小提琴:http: //jsfiddle.net/infatti/tdvHJ/1/

Eq(0) 选择第一个元素... ;)

于 2013-09-24T15:00:06.917 回答
0

尝试:

$('.focus:visible:first').filter(":input").focus();

jsFiddle 示例

于 2013-09-24T15:01:16.300 回答
0

我不确定您要达到什么目标,因为没有任何以课程为重点的元素。只需将类焦点添加到您的输入元素,然后我们

$('.focus').focus();
于 2013-09-24T15:02:15.090 回答