代码之家  ›  专栏  ›  技术社区  ›  Saeed Heidarizarei

树结构的多个javascript切换

  •  0
  • Saeed Heidarizarei  · 技术社区  · 5 年前

    我有一个无限节点的树结构,如何放置 Togglable content 在每个节点下? 我的意思是 可切换内容 对于所有节点都是相同的。 我设定了 min-width min-height 对于 .tree li a 我想要 可切换内容 在下面 树李A .
    现在切换1个节点的工作,而这不在每个节点下。
    照片: photo

        var toggle = document.getElementById("toggle");
        var content = document.getElementById("content");
    
        toggle.addEventListener("click", function () {
          content.classList.toggle("appear");
        }, false);
      body {
          font-family: sans-serif;
          font-size: 15px;
        }
    
        .tree {
          transform: rotate(0deg);
          transform-origin: 50%;
        }
    
        .tree ul {
          position: relative;
          padding: 1em 0;
          white-space: nowrap;
          margin: 0 auto;
          text-align: center;
        }
    
        .tree ul::after {
          content: '';
          display: table;
          clear: both;
        }
    
        .tree li {
          display: inline-block;
          vertical-align: top;
          text-align: center;
          list-style-type: none;
          position: relative;
          padding: 1em 0.5em 0 0.5em;
        }
    
        .tree li::before,
        .tree li::after {
          content: '';
          position: absolute;
          top: 0;
          right: 50%;
          border-top: 1px solid #ccc;
          width: 50%;
          height: 1em;
        }
    
        .tree li::after {
          right: auto;
          left: 50%;
          border-left: 1px solid #ccc;
        }
    
        .tree li:only-child::after,
        .tree li:only-child::before {
          display: none;
        }
    
        .tree li:only-child {
          padding-top: 0;
        }
    
        .tree li:first-child::before,
        .tree li:last-child::after {
          border: 0 none;
        }
    
        .tree li:last-child::before {
          border-right: 1px solid #ccc;
          border-radius: 0 5px 0 0;
        }
    
        .tree li:first-child::after {
          border-radius: 5px 0 0 0;
        }
    
        .tree ul ul::before {
          content: '';
          position: absolute;
          top: 0;
          left: 50%;
          border-left: 1px solid #ccc;
          width: 0;
          height: 1em;
        }
    
        .tree li a {
          min-width: 16em;
          min-height: 5em;
          border: 1px solid #ccc;
          padding: 0.5em 0.75em;
          text-decoration: none;
          display: inline-block;
          border-radius: 5px;
          color: #333;
          position: relative;
          top: 1px;
          transform: rotate(0deg);
        }
    
        .tree li a:hover,
        .tree li a:hover+ul li a {
          background: #e9453f;
          color: #fff;
          border: 1px solid #e9453f;
        }
    
        .tree li a:hover+ul li::after,
        .tree li a:hover+ul li::before,
        .tree li a:hover+ul::before,
        .tree li a:hover+ul ul::before {
          border-color: #e9453f;
        }
    
        #content {
          /* DON'T USE DISPLAY NONE/BLOCK! Instead: */
          background: #cf5;
          padding: 10px;
          position: inherit;
          visibility: hidden;
          opacity: 0.4;
          transition: 0.6s;
          -webkit-transition: 0.6s;
          transform: translateY(-20%);
          -webkit-transform: translateY(-20%);
        }
    
        #content.appear {
          visibility: visible;
          opacity: 1;
          transform: translateX(0);
          -webkit-transform: translateX(0);
        }
    <body>
      <div class="row">
        <div class="col-sm-12 text-center tree">
          <ul>
            <li id="toggle">
              <a href="#">parent</a>
              <ul>
                <li id="toggle">
                  <a href="#">boy</a>
                </li>
                <li id="toggle">
                  <a href="#">girl</a>
                </li>
              </ul>
            </li>
          </ul>
    
          <div id="content">This Togglable content is same for all id="toggle"</div>
    
        </div>
      </div>
    
      
    </body>
    3 回复  |  直到 5 年前
        1
  •  1
  •   Kresimir    5 年前

    不要用同样的 id 对于多个元素, 身份证件 应该总是独一无二的。

    使用 class 相反。

    <li class="toggle"> ... </li>
    

    然后您可以在javascript中创建一个对象数组来访问这些元素:

    let toggle_list = document.querySelectorAll(".toggle");
    

    然后你可以使用 for 循环(或 forEach 方法,如果您想花哨的话)为每个方法分配一个事件侦听器:

    for(let i = 0; i < toggle_list.length; i++) {
      toggle_list[i].addEventListener("click", toggleClick);
    }
    

    现在可以定义toggleClick函数,如下所示:

    function toggleClick(e) {
      e.target.children[0].classList.toggle("appear");
    }
    

    在这个例子中, children[0] 将目标放在 <li> 单击,并且该子元素不需要单独的 class="content" 属性。如果不是你想要的,你可以把它换成别的东西(也许 children[1] D)。

    更多信息: https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll

        2
  •  0
  •   sbqq    5 年前

    据我所知 id 属性对于HTML文档应该是唯一的

    你可以试着用 data- 属性,然后匹配元素,例如 data-toggle

    link for documentation

        3
  •  0
  •   Saeed Heidarizarei    5 年前

    ___通过以下方式解决:

      function toggleDocs(event) {
    var next = event.target.nextElementSibling;
    if (next.style.display == "none") {
      next.style.display = "block";
    
    } else {
      next.style.display = "none";
    }
      }
      document.addEventListener('click', toggleDocs, true);
      body {
          font-family: sans-serif;
          font-size: 15px;
        }
    
        .tree {
          transform: rotate(0deg);
          transform-origin: 50%;
        }
    
        .tree ul {
          position: relative;
          padding: 1em 0;
          white-space: nowrap;
          margin: 0 auto;
          text-align: center;
        }
    
        .tree ul::after {
          content: '';
          display: table;
          clear: both;
        }
    
        .tree li {
          display: inline-block;
          vertical-align: top;
          text-align: center;
          list-style-type: none;
          position: relative;
          padding: 1em 0.5em 0 0.5em;
        }
    
        .tree li::before,
        .tree li::after {
          content: '';
          position: absolute;
          top: 0;
          right: 50%;
          border-top: 1px solid #ccc;
          width: 50%;
          height: 1em;
        }
    
        .tree li::after {
          right: auto;
          left: 50%;
          border-left: 1px solid #ccc;
        }
    
        .tree li:only-child::after,
        .tree li:only-child::before {
          display: none;
        }
    
        .tree li:only-child {
          padding-top: 0;
        }
    
        .tree li:first-child::before,
        .tree li:last-child::after {
          border: 0 none;
        }
    
        .tree li:last-child::before {
          border-right: 1px solid #ccc;
          border-radius: 0 5px 0 0;
        }
    
        .tree li:first-child::after {
          border-radius: 5px 0 0 0;
        }
    
        .tree ul ul::before {
          content: '';
          position: absolute;
          top: 0;
          left: 50%;
          border-left: 1px solid #ccc;
          width: 0;
          height: 1em;
        }
    
        .tree li a {
          min-width: 16em;
          min-height: 5em;
          border: 1px solid #ccc;
          padding: 0.5em 0.75em;
          text-decoration: none;
          display: inline-block;
          border-radius: 5px;
          color: #333;
          position: relative;
          top: 1px;
          transform: rotate(0deg);
        }
    
        .tree li a:hover,
        .tree li a:hover+ul li a {
          background: #e9453f;
          color: #fff;
          border: 1px solid #e9453f;
        }
    
        .tree li a:hover+ul li::after,
        .tree li a:hover+ul li::before,
        .tree li a:hover+ul::before,
        .tree li a:hover+ul ul::before {
          border-color: #e9453f;
        }
    
        #content {
          /* DON'T USE DISPLAY NONE/BLOCK! Instead: */
          background: #cf5;
          padding: 10px;
          position: inherit;
          visibility: hidden;
          opacity: 0.4;
          transition: 0.6s;
          -webkit-transition: 0.6s;
          transform: translateY(-20%);
          -webkit-transform: translateY(-20%);
        }
    
        #content.appear {
          visibility: visible;
          opacity: 1;
          transform: translateX(0);
          -webkit-transform: translateX(0);
        }
    <body>
      <div class="row">
    <div class="col-sm-12 text-center tree">
      <ul>
        <li class="clickable-heading">
          <a href="#">parent</a>
          <div style="display:none">This is Togglable 1</div>
          <ul>
            <li class="clickable-heading">
              <a href="#">boy</a>
              <div style="display:none">This is Togglable 2</div>
            </li>
            <li class="clickable-heading">
              <a href="#">girl</a>
              <div style="display:none">This is Togglable 3</div>
            </li>
          </ul>
        </li>
      </ul>
    </div>
      </div>
      </body>