我需要将焦点放在“.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();
我需要将焦点放在“.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();
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:
像这样 ?
$('.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) 选择第一个元素... ;)
我不确定您要达到什么目标,因为没有任何以课程为重点的元素。只需将类焦点添加到您的输入元素,然后我们
$('.focus').focus();