简体   繁体   English

如何只选择DOM中元素之前的兄弟姐妹?

[英]How to select only the siblings that are before the element in the DOM?

When I hover over a ring I would like the ring hovered and all the rings under it to be colored in as well. 当我将鼠标悬停在戒指上时,我希望将戒指悬停并且其下的所有戒指也要上色。 For example: If I hover over ring_two_fill_1 I would like ring_one_fill_1 to be filled in as well as ring_two_fill_1. 例如:如果我将鼠标悬停在ring_two_fill_1上方,则我希望同时填充ring_one_fill_1和ring_two_fill_1。 I am making a progress graphic that displays the level you are at. 我正在制作一个进度图形,显示您所在的级别。

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="615.7px"
 height="621px" viewBox="0 0 615.7 621" enable-background="new 0 0 615.7 621" xml:space="preserve">

    <g id="level_one">
        <path id="ring_three_fill_1" class="ring-fill"/>
        <path id="ring_two_fill_1" class="ring-fill"/>
        <path id="ring_one_fill_1" class="ring-fill"/>
    </g>
</svg>

Yea, you'll basically have to end up using next() method. 是的,您基本上必须最终使用next()方法。 Here's an example of such functionality for a simple list: 这是用于简单列表的此类功能的示例:

HTML: HTML:

<ul>
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
    <li>Four</li>
</ul>

CSS: CSS:

  ul {
    list-style: none;
    width: 50%;
    text-align: center;
  }
  ul li {
    padding: 10px;
    margin: 2px 0;
    background-color: lightpink;
  }
  ul li.active {
    background-color: lightgreen;
  }

jQuery: jQuery的:

var lis = $('li');
function hoverIn () {
    $(this).addClass("active");
    if($(this).next().length != 0) {
        hoverIn.call($(this).next(), null); 
    } else {
        return;
    }
}
function hoverOut () {
    lis.removeClass("active");
}
lis.on("mouseenter", hoverIn);
lis.on("mouseleave", hoverOut);

Try to use Jquery next() method and hover event : 尝试使用Jquery next()方法和hover事件:

$('body').on('hover', '.ring-fill', function(){
     $(this).css('color', 'GREEN');
     $(this).next('.ring-fill').css('color', 'GREEN');
});

NOTE : You miss double quote " after avery class="ring-fill . 注意:在平均值class="ring-fill "之后,您会错过双引号"

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM