2016-03-10 3 views
-1

Я хочу изменить цвет li элементов, если текст в том, что li элемент is "OFF".jQuery change css ul nodes

HTML структура:

-> OL 
    -> UL 
     -> UL 
     -> LI - if LI text is "OFF" its color should be red. 
    -> UL 
    -> UL 
     -> LI 

я сделал что-то подобное, но не работает:

$("#content").children().each(function() { 
    $(this).children().each(function() { 
    if (this.text == "OFF") 
     $(this).css({ 'color': 'red' }); 
    }) 
}) 
+0

Что вы имеете в виду текст выключен? Не могли бы вы предоставить свой HTML? – Aaron

+0

@Aaron Если текстовое содержимое «ВЫКЛ». –

ответ

1

Вы делаете это неправильно. Вам нужно сделать так:

$("#content").find("li").each(function() { 
    if ($(this).text().trim() == "OFF") 
    $(this).css({ 'color': 'red' }); 
}); 

Отрывок

$(function() { 
 
    $("#content").find("li").each(function() { 
 
    if ($(this).text().trim() == "OFF") 
 
     $(this).css({ 'color': 'red' }); 
 
    }); 
 
});
<script src="https://code.jquery.com/jquery-2.1.4.js"></script> 
 
<ol id="content"> 
 
    <li> 
 
    <ul> 
 
     <li>OFF</li> 
 
    </ul> 
 
    </li> 
 
    <li> 
 
    <ul> 
 
     <li>ON</li> 
 
    </ul> 
 
    </li> 
 
</ol>