我可以同时对一种以上的元素类型使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:51:11  来源:igfitidea点击:

Can I use jQuery find() for more than one element type at the same time?

jquery

提问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 inputor 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.

有没有办法可以更改此功能,使其适用于 aninputtextarea?我认为这样做的方法是更改​​,var tds = $(this).find('input'),但是我不确定如何更改它,或者即使可以更改为“查找”文本区域或输入。

回答by Ry-

Use a comma to union multiple queries:

使用逗号联合多个查询:

var tds = $(this).find('input, textarea');

You can also use :inputas 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 tds child whatever it is

您可以使用.children()来选择tds 孩子,无论它是什么

var tds = $(this).children();

回答by Jashwant

You cannot add a textareawith the same idas another element. Its incorrect. idvalues are unique for a single html element within the document.

您不能添加textareaid另一个元素相同的。它不正确。id值对于文档中的单个 html 元素是唯一的。

I would suggest to you classinstead.

我会建议你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')