简体   繁体   中英

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

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:

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 ".

The second one simply says, find all td s and exclude the first lot from the matches :)

You can use $.grep() to filter your array.

Using the function passed to $.grep() you can try and find your elements within the current <td> . If neither a.edit-row or a.delete-row are found return true, otherwise return false:

var filteredTds = $.grep(jqTds, function(td){
    var $td = $(td);
    return !$td.find('a.edit-row').length && !$td.find('a.delete-row').length;
});

JSFiddle

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>';
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM