如何在页面上创建子文件夹?

时间:2016-07-25 22:55:15

标签: javascript html css

我不知道如何处理这个问题。我希望能够执行以下操作:我有一个链接到另一个页面的文件夹的图标。当用户单击图标而不是转到页面时,文件夹图标下方会显示一个子文件夹,当用户单击其中一个文件夹时,它会将用户定向到该页面。

以下是我所做的:

<h4>
  <a href="CalMediConnect_DMgmt.cfm">
    <img src="images/folder-documents-icon32.jpg" alt="Custodial" class="float-left">
    &nbsp;Disease Management
  </a>
</h4> 
<br /><br />

更新

我尝试了以下操作,似乎无法正常工作:

以下是脚本:

var tree = document.querySelectorAll('ul.tree a:not(:last-child)');
for(var i = 0; i < tree.length; i++) {
  tree[i].addEventListener('click', function(e) {
    var parent = e.target.parentElement;
    var classList = parent.classList;
    if(classList.contains("open")) {
      classList.remove('open');
      var opensubs = parent.querySelectorAll(':scope .open');
      for(var i = 0; i < opensubs.length; i++) {
        opensubs[i].classList.remove('open');
      }
    } else {
      classList.add('open');
    }
  });
}

以下是CSS:

ul.tree li {
  list-style-type: none;
  position: relative;
}

ul.tree li ul {
  display: none;
}

ul.tree li.open > ul {
  display: block;
}

ul.tree li a {
  color: #4284B0;
  text-decoration: none;
}

ul.tree li a:before {
  height: 1em;
  padding: .1em;
  font-size: .8em;
  display: block;
  position: absolute;
  left: -1.3em;
  top: .2em;
}
.margin-left {
  margin-left: -15px;
}

HTML:

<ul class="tree margin-left">
  <li>
    <h4>
      <a href="#">
        <img src="images/folder-documents-icon32.jpg" alt="Custodial" class="float-left">
        &nbsp;Disease Management
      </a>
    </h4>
    <ul>
      <li>
        <h5>
          <a href="CalMediConnect_DMgmt.cfm">
            <img src="images/folder-documents-icon32.jpg" alt="Custodial" class="float-left">
            &nbsp;Disease Management
          </a>
        </h5>
      </li>
    </ul>
  </li> 
</ul>

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

这是一个使用jQuery构建的基本示例...

https://jsfiddle.net/kennethcss/8b4e6o42/

&#13;
&#13;
$('.folder').on('click', function(e) {
  var folder = $(this).find('.sub-folder');

  if (e.target !== this) return;

  if(folder.hasClass('hidden')) {
    folder.removeClass('hidden');
  } else {
    folder.addClass('hidden');
  }
});
&#13;
.folder {
  cursor: pointer;
}

.hidden {
  display: none;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>

<ul class="container">
  <li class="folder">Primary
    <ul class="sub-folder hidden">
      <li>Secondary</li>
      <li>Secondary</li>
      <li>Secondary</li>
    </ul>
  </li>
  <li class="folder">Primary
    <ul class="sub-folder hidden">
      <li>Secondary</li>
      <li>Secondary</li>
      <li>Secondary</li>
    </ul>
  </li>
  <li class="folder">Primary
    <ul class="sub-folder hidden">
      <li>Secondary</li>
      <li>Secondary</li>
      <li>Secondary</li>
    </ul>
  </li>
</ul>
&#13;
&#13;
&#13;

当然,你可以随心所欲地设计风格;这个例子只是演示了如何构建HTML,CSS和JS来创建一个简单的文件夹结构。

<强>主旨

https://gist.github.com/kennethcss/8db1dc3326917c77846e84d263beb67d