纯 JavaScript 可折叠列表

Pure JavaScript collapsible list

我正在开发一个 JavaScript 函数来将 CSS 显示 属性 从 none 切换到阻止。我可以让一个列表项进行切换,但我无法让后续列表项进行切换。我的目标是在使用 bootstrap 之前学习使用纯 JavaScript - 但也许我只是让它变得比需要的更难......

var options = {
  valueNames: [ 'name', 'company' ]
};

var userList = new List('contacts', options);

//This does not work
function select(detailId) {
  var x = document.getElementById(detailId).getElementsByClassName("detail");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
h2 {
  font-family:sans-serif;
}
.list {
  font-family:sans-serif;
  margin:0;
  padding:20px 0 0;
}
.list > li {
  display:block;
  background-color: #d9d9d9;
  padding:10px;
  box-shadow: inset 0 1px 0 #fff;
}
.list :hover {
  background-color: #f2f2f2;
}
.avatar {
  max-width: 150px;
}
img {
  max-width: 100%;
}
h3 {
  font-size: 16px;
  margin:0 0 0.3rem;
  font-weight: normal;
  font-weight:bold;
}
p {
  margin:0;
}

input {
  border:solid 1px #ccc;
  border-radius: 5px;
  padding:7px 14px;
  margin-bottom:10px
}
input:focus {
  outline:none;
  border-color:#aaa;
}
.sort {
  padding:8px 30px;
  border-radius: 6px;
  border:none;
  display:inline-block;
  color:#fff;
  text-decoration: none;
  background-color: #28a8e0;
  height:30px;
}
.sort:hover {
  text-decoration: none;
  background-color:#1b8aba;
}
.sort:focus {
  outline:none;
}
.sort:after {
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-bottom: 5px solid transparent;
  content:"";
  position: relative;
  top:-10px;
  right:-5px;
}
.sort.asc:after {
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 5px solid #fff;
  content:"";
  position: relative;
  top:13px;
  right:-5px;
}
.sort.desc:after {
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-bottom: 5px solid #fff;
  content:"";
  position: relative;
  top:-10px;
  right:-5px;
}
.detail {
  display: none;
}
<div id="contacts">
  <h2> Contacts</h2>
  <input class="search" placeholder="Search" />  
  <button class="sort" data-sort="name">
    Sort by name
    </button>
    <button class="sort" data-sort="company">
            Sort by Company
  </button>
  <p></p>
  <ul class="list">
    <li onclick="select(this.id)" id="1">
      <h3 class="name">John Doe</h3> 
      <span class="company">Information Services Branch</span><p></p>
      <ul class="detail" id="1">
        <li>Phone: <a href="tel:555-123-4567">555-123-4567</a></li>
        <li>Email: <a href="mailto:johndoe@email.com">johndoes@email.com</a>  
          </li>
      </ul>
    </li>
    <li onclick="select(this.id)" id="2"> 
      <h3 class="name">Jane Doe</h3> <span class="company">Department of Emergency Management</span><p>
      <ul class="detail" id="2">
        <li>
          Phone: <a href="tel:555-123-4567">555-123-4567</a>
        </li>
        <li>
          Email: <a href="mailto:janedoe@email.com">janedoes@email.com</a>
        </li>
      </ul>
      </li>
      <li onclick="select(this.id)" id="2">
        <h3 class="name">Action Jackson</h3> 
        <span class="company">Awsome, Inc.</span><p>
      <ul class="detail" id="3">
        <li>
          Phone: <a href="tel:555-123-4567">555-123-4567</a>
        </li>
        <li>
          Email: <a href="mailto:ajackson@awsomeinc.com">ajackson@awsomeinc.com</a>
        </li>
      </ul>
    </li>
  </ul>
      </div>
    </div>
    
    
  </>
</div>  
<script src="//cdnjs.cloudflare.com/ajax/libs/list.js/1.5.0/list.min.js"></script>

See initial project on Codepen. This version expands first row when you click any row. I want to click on a parent list item then expand the child list item.

看看你的 select 函数发生了什么:

function select(detailId) {

  var x = document.getElementById(detailId) // return ONE element
.getElementsByClassName("detail"); // filter it to get an ARRAY of elements

// Now you're trying to get the .style of an ARRAY
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}

最后,您尝试获取 ARRAY 的属性,这会给您带来错误。你可以做的是把 [0] 放在 .getElementsByClassName("detail") 的末尾,例如:

function select(detailId) {
  var x = document.getElementById(detailId).getElementsByClassName("detail")[0];
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}

这将获取筛选数组中的第一个元素。只要确保有任何元素即可!