创建每个兄弟姐妹的列表

时间:2016-03-09 08:16:07

标签: javascript jquery

我有多个具有多个子部分的部分。每个部分都应包含本节中所有子部分h3标题的列表。鉴于此HTML:

<div class="allwrap">
    <div class="main-section" id="m1">  
       <h2>Main-1-Headline</h2>
       <ul class="sub-sections-list">
           <!-- generate LIs of each [#main-1 > .sub-section h3 text] -->
       </ul>        
       <div class="sub-section" id="m1-s1">         
           <h3>Main-1-Sub-1-Headline</h3>
       </div>       
       <div class="sub-section" id="m1-s2">         
           <h3>Main-1-Sub-2-Headline</h3>
       </div>               
   </div>   
   <div class="main-section" id="main-2">
       <h2>Main-2-Headline</h2>
       <ul class="sub-sections-list">
           <!-- generate LIs of each [#main-2 > .sub-section h3 text] -->
       </ul>        
       <div class="sub-section" id="m2-s1">         
           <h3>Main-2-Sub-1-Headline</h3>
       </div>       
       <div class="sub-section" id="m2-s2">         
            <h3>Main-2-Sub-2-Headline</h3>
        </div>              
    </div>  
</div>

所以我遍历每个章节H3并且现在有一个所有h3标题的数组,这不是我想要完成的:

var myList = [];
$('.main-section .sub-section').each(function(){
    myList.push($(this).find('h3').text());
}); 
$.each(myList, function(){
    var li = $('<li/>').appendTo('.sub-sections-list');
    $('<a/>').attr('href', '#').text(this).appendTo(li);
});

我怎样才能使所有部分的所有h3标题都不在列表中但只包含相应父节的标题?结果应如下所示:

Main-1-Headline(Main-section-1开头)

  • 主-1-子1-标题
  • 主-1-子-2-标题

    Main-1-Sub-1-Headline(第1小节开头)

    此处有一些内容(上面的HTML不是为了简单起见)

    Main-1-Sub-2-Headline(第2小节开头)

    此处有一些内容(上面的HTML不是为了简单起见)

Main-2-Headline(Main-section-2的开头)

  • 主-2-子-1-标题
  • 主-2-子-2-标题

    Main-2-Sub-1-Headline(第1小节开头)

    此处有一些内容(上面的HTML不是为了简单起见)

    Main-2-Sub-2-Headline(第2小节开头)

    此处有一些内容(上面的HTML不是为了简单起见)

2 个答案:

答案 0 :(得分:1)

我想你想要这个。

更新了简短演示

&#13;
&#13;
$('.main-section h3').each(function(){
  $(this).parent().prevAll(".sub-sections-list").append('<li><a href="#">'+$(this).text()+'</a></li>');
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="allwrap">
  <div class="main-section" id="m1">
    <h2>Main-1-Headline</h2>
    <ul class="sub-sections-list">
      <!-- generate LIs of each [#main-1 > .sub-section h3 text] -->
    </ul>
    <div class="sub-section" id="m1-s1">
      <h3>Main-1-Sub-1-Headline</h3>
    </div>
    <div class="sub-section" id="m1-s2">
      <h3>Main-1-Sub-2-Headline</h3>
    </div>
  </div>
  <div class="main-section" id="main-2">
    <h2>Main-2-Headline</h2>
    <ul class="sub-sections-list">
      <!-- generate LIs of each [#main-2 > .sub-section h3 text] -->
    </ul>
    <div class="sub-section" id="m2-s1">
      <h3>Main-2-Sub-1-Headline</h3>
    </div>
    <div class="sub-section" id="m2-s2">
      <h3>Main-2-Sub-2-Headline</h3>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

旧方式

$('.main-section').each(function() {
  var myList = [];
  $(this).find("h3").each(function() {
    myList.push($(this).text());
  });
  $subSectionList = $(this).find('.sub-sections-list');
  $.each(myList, function() {
    var li = $('<li/>').appendTo($subSectionList);
    $('<a/>').attr('href', '#').text(this).appendTo(li);
  });
});

答案 1 :(得分:0)

像这样(优化版):

abstract class Entity {
    Map<Language,MetaData> metaData;
}

class MetaData {
    private String name;
    ...setters & getters
}

class A extends Entity {
    private Set<B> b;
    private String name;
}

class B extends Entity {
    private Set<C> c;
    private String name;
}

class C extends Entity {
    private String name;
}

示例:https://jsbin.com/gefupivoji/edit?html,js,output