2

I would like to search in the HTML elements with classes to see if any element has the class span1hrfor30mins next to by an element that has the class span1hr, then change the color in each element in row3 with a class span1hr to do for each text using with the loops.

Here's a sample of what the HTML is look like:

<div id="programe1" class="pgmFirstRow div_1_2 row2 span1hrfor30mins">Test program</div>
<div id="programe2" class="pgmFirstRow div_1_3 row3 span1hr">NCIS</div>
<div id="programe3" class="pgmFirstRow div_1_4 row4 span0hr">CBS Evening News With Scott Pelley</div>
<div id="programe4" class="pgmFirstRow div_1_5 row5 span1hr">NCIS: Los Angeles</div>
<div id="programe5" class="pgmFirstRow div_1_6 row6">Person of Interest</div>
<div id="programe6" class="pgmFirstRow div_2_2 row2 span1hrfor30mins">Twisted</div>
<div id="programe7" class="pgmFirstRow div_2_3 row3 span1hr">Pretty Little Liars</div>
<div id="programe8" class="pgmFirstRow div_2_4 row4 span1hr">Pretty Little Liars</div>
<div id="programe9" class="pgmFirstRow div_2_5 row5 span1hr">Twisted</div>
<div id="programe10" class="pgmFirstRow div_2_6 row6 span1hr">Pretty Little Liars</div>
<div id="programe11" class="pgmFirstRow div_3_2 row2 span1hr">CNN Newsroom</div>
<div id="programe12" class="pgmFirstRow div_3_3 row3 span1hr">Around the World</div>
<div id="programe13" class="pgmFirstRow div_3_4 row4 span0hr">CNN Newsroom</div>
<div id="programe14" class="pgmFirstRow div_3_5 row5 span2hr">CNN Newsroom</div>
<div id="programe15" class="pgmFirstRow div_3_6 row6 span1hr">The Lead With Jake Tapper</div>

In this case, since NCIS and Pretty Little Liars has a span1hr class, Test program and Twisted has a span1hrfor30mins class, I would like find on the sets for each element in row2 with a class span1hrfor30mins next to by row3 with a class span1hr to change the text in each row3 while the other text in the row2 remains unchanged.

I have tried the code like this, but it will search for every row2 and rows3 in the sets and it will only change the red texts in the row2 when i want to change on each text in the row3 with the class span1hr next to by the row2 with a class span1hrfor30mins.

$('.span1hrfor30mins.row2').each(function (i, e) {
    $('+.span1hr.row3', e).length && $(e).css('color', 'red');
});

Here is an example jsfiddle: http://jsfiddle.net/REjC5/6/

Does anyone know how I can do this?

4

1 回答 1

1

使用需要使用next方法来定位您正在寻找的元素。

next获取该元素之后的直接兄弟元素。

$('.span1hrfor30mins.row2').each(function (i, e) {
    $(this).next('.span1hr.row3').css('color', 'red');
});

检查小提琴

于 2013-07-21T00:34:49.737 回答