【问题标题】:Dynamically addClass() to elements depending on the siblings previous class?根据兄弟姐妹上一个类动态地将 addClass() 添加到元素?
【发布时间】:2016-05-26 10:56:24
【问题描述】:

我有一个<ul> 元素,里面有<li>。列表项要么有:

<li class="folder"> 
<!-- or... -->
<li class="file">

在每个folder's 中嵌套了一个color-label

<li class="folder">
    Folder 1 
    <i class="blue"></i>
</li>

我需要使用 jQuery 动态查看所有 &lt;li class="folder"&gt; 列表项并找到 &lt;i class="color"&gt; 标签,然后将该类应用于每个 &lt;li class="file"&gt; 兄弟姐妹,直到找到下一个文件夹颜色标签,然后重复该过程,直到所有 &lt;li&gt; 都具有正确的颜色,具体取决于文件夹的 color-label 类。

下面是一个示例:

我尝试使用.nextUntil() jquery 方法,但没有得到最好的结果。这是我的代码:

https://jsfiddle.net/5c11sqL5/

<nav class="directory">
    <h1 class="title">Directory</h1>
    <ul class="container">
        <!-- Folder 1 -->  
        <li class="folder">Folder 1 <i class="blue"></i></li>
        <li class="file">File 1</li>
        <li class="file">File 2</li>
        <li class="file">File 3</li>

        <!-- Folder 2 -->  
        <li class="folder">Folder 2 <i class="green"></i></li>
        <li class="file">File 1</li>
        <li class="file">File 2</li>
        <li class="file">File 3</li>

        <!-- Folder 3 -->  
        <li class="folder">Folder 3 <i class="gray"></i></li>
        <li class="file">File 1</li>
        <li class="file">File 2</li>
        <li class="file">File 3</li>
    </ul>
</nav>
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: "Courier New", Courier, monospace;
}

/* Directory */

.directory {
    display: flex;
    flex-direction: column;
    background: #ccc;
    width: 240px;
    height: 100vh;
}

.directory .title {
    letter-spacing: 3px;
    text-transform: uppercase;
    opacity: .07;
    text-align: center;
    margin: 1rem 0 0;
}

.directory .container {
    list-style: none;
}

/* Folder and Files */

.folder {
    font-weight: bold;
    font-size: 1rem;
    position: relative;
    padding: 0 0 0 1rem;
    margin: 1rem 0 0;
}

.file {
    font-size: .8rem;
    padding: 0 0 0 1rem;
}

/* Colors */

.blue,
.green,
.gray {
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    width: 5px;
    display: block;
}

.blue {
    background: rgba(0, 194, 224, 1);
}

.green {
    background: rgba(81, 232, 152, 1);
}

.gray {
    background: rgba(131, 140, 145, 1);
}

【问题讨论】:

  • Rory McCrossan 的解决方案是正确的。如果我是你,我会改变 HTML 的布局,所以 li.folder 嵌套了 &lt;ul&gt; 和嵌套的 li.file,这样你就可以轻松选择文件夹文件,如 $('li.folder').each(function(folder) { var files = $('li.file', folder), className = $('i', folder).prop('class'); files.addClass(className) })
  • 我完全同意!唯一的问题是 html 是为我正在制作的 Trello 扩展动态编写的。 “trello 列表”是目录,“trello 卡”是文件夹或文件:oneezy.com/www/gtd/trello-directory.png。这些卡片是可拖放的,并且有很多与它们相关的“trello”东西,所以我必须非常小心我做了多少 DOM 操作。

标签: javascript jquery css addclass nextuntil


【解决方案1】:

您可以通过循环遍历每个 i 元素然后将其类名复制到后面的每个 .file 来实现这一点,如下所示:

$('.directory .folder i').each(function() {
    var className = $(this).prop('class');
    $(this).closest('li').nextUntil('.folder').addClass(className);
})

另请注意,您需要修改已定义的类,以便 position: absolute 规则仅适用于 i 元素,如下所示:

i.blue,
i.green,
i.gray {
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    width: 5px;
    display: block;
}
i.blue {
    background: rgba(0, 194, 224, 1);
}
i.green {
    background: rgba(81, 232, 152, 1);
}
i.gray {
    background: rgba(131, 140, 145, 1);
}

.file.blue {
    color: rgba(0, 194, 224, 1);
}
.file.green {
    color: rgba(81, 232, 152, 1);
}
.file.gray {
    color: rgba(131, 140, 145, 1);
}

Updated fiddle

【讨论】:

  • 为什么要为同一种颜色创建两条规则?你可以只创建.color,因为你指定绝对元素是i.color
  • 太棒了!感谢您的帮助。
  • @Sotiris 因为i.colour 设置background.file.colour 设置color
  • @Oneezy 没问题,很高兴为您提供帮助
猜你喜欢
  • 2017-07-16
  • 1970-01-01
  • 1970-01-01
  • 2014-07-12
  • 2012-07-23
  • 1970-01-01
  • 2013-12-13
  • 2021-02-25
  • 1970-01-01
相关资源
最近更新 更多