简体   繁体   中英

Highlight several element with single mousover/hover event. Jquery

Is there any chance with mouseover (or any other) event on certain element to get following html

eg highlight all previous cells:

start: class="selected" ,

finish: cell with onmouseover event

#if mouse pointer is on the cell #4
<li id="1" class="reg"></li>
<li id="2" class="selected"></li>
<li id="3" class="in_range"></li>
<li id="4" class="in_range"></li>
<li id="5" class="reg"></li>
<li id="6" class="reg"></li>
<li id="7" class="reg"></li>
<li id="8" class="reg"></li>
<li id="9" class="reg"></li>
<li id="10" class="reg"></li>

#if mouse pointer is on the cell #6
<li id="1" class="reg"></li>
<li id="2" class="selected"></li>
<li id="3" class="in_range"></li>
<li id="4" class="in_range"></li>
<li id="5" class="in_range"></li>
<li id="6" class="in_range"></li>
<li id="7" class="reg"></li>
<li id="8" class="reg"></li>
<li id="9" class="reg"></li>
<li id="10" class="reg"></li>


#if mouse pointer is on the cell #8
<li id="1" class="reg"></li>
<li id="2" class="selected"></li>
<li id="3" class="in_range"></li>
<li id="4" class="in_range"></li>
<li id="5" class="in_range"></li>
<li id="6" class="in_range"></li>
<li id="7" class="in_range"></li>
<li id="8" class="in_range"></li>
<li id="9" class="reg"></li>
<li id="10" class="reg"></li>

As you said to target all Previous Elements...

$("li").hover(function(){
    $(this).prevAll().not(":first").addClass("in_range");
});

UPDATE

WORKING FIDDLE

Use .prevUntil() to find out previous siblings until you reach first one. For that you have to change class name of first <li> as i have done in UPDATED FIDDLE.

$("li").hover(function(){
    $(this).prevUntil(".not").addClass("in_range");
});

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