收集所有“可见"内容的最佳方法没有特定类的jQuery表单元素? [英] Best way to collect all "visible" form elements with jQuery that don't have a specific class?
问题描述
我正在尝试在给定的表单上重新分配新的tab-index.为此,我要排除任何不可见(不可见)的表单元素-并且还排除具有特定类(".offscreen")的任何表单元素.
I'm trying to re-assign a new tab-index on a given form. To do this I want to exclude any form elements that are invisible (not visible) -- and also exclude any form elements that possess a specific class (".offscreen").
我正在尝试这种方法-但是,它不起作用(也许不是最有效的方法).
I'm trying this method -- but, it's not working (and is perhaps not the most efficient method).
function reassignTabOrders() {
var tabindex = 1;
$j('input,select,textarea').not('.offscreen').each(function() {
var $input = $j(this);
if ($input.is(':visible')) {
$input.attr("tabindex", tabindex);
tabindex++;
}
});
};
有什么想法吗?
推荐答案
您可以使用 :visible
选择器和 :input
,如下所示:
You can use the :visible
selector and :input
, like this:
function reassignTabOrders() {
$j(':input:visible:not(.offscreen)').each(function(i) {
this.tabIndex = i+1;
});
}
此操作还将 .each()
函数提供的索引用于回调,无需您自己维护一个单独的变量:)在更高版本的jQuery中,您可以使用带有 .attr()
,就像这样:
This also uses the index provided by the .each()
function to the callback, no need to maintain a separate variable yourself :) In later jQuery versions you can shorten it down (but not quite as fast) using a function with .attr()
, like this:
function reassignTabOrders() {
$j(':input:visible:not(.offscreen)').attr("tabindex", function(i) {
return i+1;
});
}
这篇关于收集所有“可见"内容的最佳方法没有特定类的jQuery表单元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!