简体   繁体   English

突出显示列表的父母,但不是所有孩子

[英]Highlight parents of list but not all children

I have created a <ul> element and what I am trying to do is to highlight the list elements from a certain child and all the way up. 我创建了一个<ul>元素,而我想要做的是突出显示某个孩子的列表元素,直到整个过程。 However, because of the nested children, when I highlight a parent also all its children are highlighted (while I want to highlight only the text of the parents). 但是,由于嵌套的子项,当我突出显示父项时,也会突出显示其所有子项(而我只想突出显示父项的文本)。

https://jsfiddle.net/zcfvuh6h/3/ https://jsfiddle.net/zcfvuh6h/3/

In this example, I should get the nodes Four12, Four1 and Four highlighted. 在此示例中,我应该使节点Four12,Four1和Four突出显示。

Any suggestions? 有什么建议么? Thank you. 谢谢。

EDIT: 编辑:

Okay, so after understanding what the actual problem you are trying to solve is, it took a bit of work, but I got a working solution. 好的,在了解了您要解决的实际问题之后,我们做了一些工作,但是我得到了一个可行的解决方案。

Working DEMO 工作演示

A few things to note 注意事项

1. All of your text in your <li> need to be in a container of some sort, a <span> is fine. 1. <li>所有文本都必须放在某种容器中, <span>可以。 You had some in spans and some not, so I put them all in spans for you. 您有些跨度,有些则没有,所以我把它们全部跨度给您。

2. This cannot be done with background-color on the <li> or <ul> because it spans multiple lines if it has children. 2.这不能用<li><ul>上的background-color完成,因为如果有子<ul> ,它将跨越多行。 You have to use a css pseudo-element in order to get the desired effect. 您必须使用css pseudo-element才能获得所需的效果。

3. The demo I have posted also dynamically sets the background of the element and parents based on which element you click on. 3.我发布的演示还根据您单击的元素动态设置了元素的背景和父元素。 You must click on a list item in order for the backgrounds colors to show up. 您必须单击一个列表项才能显示背景颜色。

4. Your d3 code that you included is all obsolete at this point. 4.此时包含的d3代码已经过时了。 It can be done with 7 toal lines of jQuery. 可以使用jQuery的7条提示行来完成。

5. Enjoy! 5.享受!

HTML HTML

...
  <li id="i6"><span class="listItem">Four</span>
    <ul>
      <li id="i7" class="listItem"><span class="listItem">Four1</span>
          <ul>
            <li id="i71" class="listItem"><span class="listItem">Four11</span>
               <ul>
                  <li id="i4111" class="listItem"><span class="listItem">Four111</span></li>
                  <li id="i4112" class="listItem"><span class="listItem">Four112</span></li>
               </ul>
              </li>        
            <li id="i12" class="listItem"><span class="listItem">Four12</span></li>
          </ul>
      <li class="listItem"><span class="listItem">Five</span></li>
    </ul>
  </li>
...

Javascript Java脚本

$(function () {
  $(".listItem:not(li)").on("click", function () {
    var parentListItem = $(this).parent();
    $("#menu1 .highlight").removeClass("highlight");
    parentListItem.addClass("highlight").parents("li").addClass("highlight");
  });
});

CSS CSS

.highlight {
  position: relative;
}
.highlight > * {
  position: relative;
  z-index: 100;
}
.highlight::before {
  content: ' ';
  background-color: cyan;
  width: 100%;
  height: 15px;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
}

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

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