select matching elements whose sibling element has a specific class

时间:2015-10-30 23:03:58

标签: jquery

I want to select all links that a direct child of a table cell, so I have the following selector:

$('td a')

But I want to add another filter to this. I only want to select those table cell links that have a direct sibling with a class of 'moda'. In other words, the following will match:

<td>
  <a data-target="#unresolvedTasks" data-toggle="modal" href="#" style="">dfsgdfg fghfghjhgj</a>
  <div class="modal fade" id="unresolvedTasks" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
</td>

I did a google search about AND conditions, which gave me this link. But this is a different type of AND condition:

a=$('[myc="blue"][myid="1"][myid="3"]');

That will select an element that meets all those criteria. I want to select an element that meets a specific criteria and then also check if its sibling meets a specific criteria.

I tried this:

$('td a, td a + div[class="modal"]')

But that is doing an OR condition not an AND condition.

Any suggestions?

2 个答案:

答案 0 :(得分:0)

try to use

$('td > div.modal').prev('a')

and if you want to loop through them

$('td').each(function(){
    console.log($(this).find('div.modal').prev('a').attr('data-target'));
});

答案 1 :(得分:0)

result = [{quote.id:quote.qualification_set.all()} for quote in Quote.objects.all()]

演示 http://jsfiddle.net/phv12hbu/