[英]jquery help to get element in array that contains certain class
jqTds =[
<td class="hidden-xs sorting_1">text1</td>
<td class=" ">text2</td>
<td class=" ">text3</td>
<td class=" ">text4</td>
<td class=" "><a href="#" class="edit-row">Edit</a></td>
<td class=" "><a href="#" class="delete-row">Delete</a></td>]
How can I get all elements that has a "anchor" with class "edit-row" or "delete-row" and get all other that does not have it如何获取所有具有“锚点”类“编辑行”或“删除行”的元素,并获取所有其他没有它的元素
// I am editing a script that uses DataTables.Js what I am trying to do is getting all elements from a table row into (var jqTds = $('>td:not(.hide_me)', nRow)) and now I want to include an input in all elements except the ones that has save-row class and edit-row class cos they are link to save/delete // 我正在编辑一个使用 DataTables.Js 的脚本,我想要做的是将表行中的所有元素放入 (var jqTds = $('>td:not(.hide_me)', nRow)) 中,现在我想要在所有元素中包含一个输入,除了那些具有保存行类和编辑行类的元素,因为它们链接到保存/删除
thanks in advance提前致谢
No idea why you have them in "an array", but if you run this while they are still in the DOM, use the :has
pseudo selector:不知道为什么将它们放在“数组”中,但是如果您在它们仍在 DOM 中时运行它,请使用:has
伪选择器:
var $tds = $('td:has(a:.edit-row,a:delete-row)');
var $otherTds = $('td').not($tds);
The first one reads.第一个读。 *find any td that has an anchor within it with class edit-row
or an anchor within it with class delete-row
". *发现有类中它的锚任何TD edit-row
或类中它的锚delete-row
”。
The second one simply says, find all td
s and exclude the first lot from the matches :)第二个简单地说,找到所有td
并从匹配中排除第一批:)
You can use $.grep()
to filter your array.您可以使用$.grep()
来过滤您的数组。
Using the function passed to $.grep()
you can try and find your elements within the current <td>
.使用传递给$.grep()
的函数,您可以尝试在当前<td>
找到您的元素。 If neither a.edit-row
or a.delete-row
are found return true, otherwise return false:如果a.edit-row
和a.delete-row
都没有找到,则返回 true,否则返回 false:
var filteredTds = $.grep(jqTds, function(td){
var $td = $(td);
return !$td.find('a.edit-row').length && !$td.find('a.delete-row').length;
});
I was able to do like this:我能够这样做:
for (var i = 0; i < editColumn.length; i++) {
if ($(editColumn[i]).find("a").hasClass("edit-row") == true) {
editColumn[i].innerHTML = '<a class="save-row" href="">Salvar</a>';
}
else if ($(editColumn[i]).find("a").hasClass("delete-row") == true) {
editColumn[i].innerHTML = '<a class="cancel-row" href="">cancelar</a>';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.