Return values of elements without a given class

2

I have a list with images, and I need to return a value of those images to then assign some commands. The problem is that in some <li> there will be a class named off and needs to be "discarded" from the order.

Sample code:

<ul>
    <li class="off">
        <img src="image1.jpg" class="imgx" />
    </li>
    <li>
        <img src="image1.jpg" class="imgx" />
    </li>
    <li class="off">
        <img src="image1.jpg" class="imgx" />
    </li>
    <li class="off">
        <img src="image1.jpg" class="imgx" />
    </li>
    <li>
        <img src="image1.jpg" class="imgx" />
    </li>
</ul>

That is, I need to list the <img> that are not within <li class="off"> .

I've tried something like:

var teste = $('li:not(:has(.off)) > img.imgx').eq(1).attr('src');

But it seems that you are not "excluding" those with class off .

    
asked by anonymous 15.10.2014 / 15:18

1 answer

2

I used the li:not(.off) > img.imgx selector that selects all elements with class imgx whose parent is a li that does not have the off class.

The problem you have is due to the accumulation of two pseudo-selectors, which is not necessary in this case.

In the example below, to illustrate, I add a class selecionad to highlight the result of the selector.

$('li:not(.off) > img.imgx').addClass('selecionado');
.selecionado {
  background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><ul><liclass="off">
        <img src="image1.jpg" class="imgx" />
    </li>
    <li>
        <img src="image1.jpg" class="imgx" />
    </li>
    <li class="off">
        <img src="image1.jpg" class="imgx" />
    </li>
    <li class="off">
        <img src="image1.jpg" class="imgx" />
    </li>
    <li>
        <img src="image1.jpg" class="imgx" />
    </li>
</ul>
    
15.10.2014 / 15:28