我可以同时对一种以上的元素类型使用 jQuery find() 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11698448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I use jQuery find() for more than one element type at the same time?
提问by Alan2
I am using the following function to change the padding in a column on my forms:
我正在使用以下函数来更改表单列中的填充:
function padTitles() {
$('#option-grid #dataTable tr, #topic-grid #dataTable tr')
.each(function () {
var tds = $(this).find('input'),
text = tds.filter('[id^="input_TempRowKey_"]').val(),
tdToPad = tds.filter('[id^="input_Title_"]'),
pad;
if (/\.0$/.test(text)) {
pad = 10;
level = 1;
} else {
pad = 35;
level = 2;
}
tdToPad.css('margin-left', pad);
a = tdToPad.closest('tr');
if (tdToPad.closest('tr').get().className) {
tdToPad.closest('tr').get().className = tdToPad.closest('tr').get().className.replace(/\blevel\-.*?\b/g, 'level-' + level);
} else {
tdToPad.closest('tr').addClass('level-' + level)
}
});
}
It works well for this form HTML:
它适用于这种表单 HTML:
<td id="title_1" class=" ">
<input type="text" value="Tests" name="item.Title" id="input_Title_1" >
</td>
Now I would also like it to work for the following HTML:
现在我还希望它适用于以下 HTML:
<td id="title_1" class=" ">
<textarea name="item.Title" id="input_Title_1">Tests</textarea>
</td>
Is there a way I can change this function so it works for either an input
or textarea
? I assume the way to do this is to change var tds = $(this).find('input'),
however I am not sure how to change it or even if it's possible to change to "find" either a textarea or an input.
有没有办法可以更改此功能,使其适用于 aninput
或textarea
?我认为这样做的方法是更改,var tds = $(this).find('input'),
但是我不确定如何更改它,或者即使可以更改为“查找”文本区域或输入。
回答by Ry-
Use a comma to union multiple queries:
使用逗号联合多个查询:
var tds = $(this).find('input, textarea');
You can also use :input
as a selector, but it's not as efficient and may also include some things you'd rather not include.
您也可以将其:input
用作选择器,但效率不高,并且还可能包含一些您不想包含的内容。
回答by Musa
You can use .children()
to select the td
s child whatever it is
您可以使用.children()
来选择td
s 孩子,无论它是什么
var tds = $(this).children();
回答by Jashwant
You cannot add a textarea
with the same id
as another element. Its incorrect. id
values are unique for a single html element within the document.
您不能添加textarea
与id
另一个元素相同的。它不正确。id
值对于文档中的单个 html 元素是唯一的。
I would suggest to you class
instead.
我会建议你class
。
<input type="text" value="Tests" class="myclass" name="item.Title" id="input_Title_1" >
<textarea name="item.Title" class="myclass" id="input_Title_2">Tests</textarea>
var tds = $(this).find('.myclass')