【问题标题】:Dynamically changing the title of a submenu动态更改子菜单的标题
【发布时间】:2020-04-25 12:23:27
【问题描述】:

我想创建一个类别下拉菜单。当页面首次加载时,我希望下拉菜单显示“类别”。我希望能够点击像“Tops”这样的类别。然后下拉菜单的标题变为“Tops”而不是类别。不太确定如何开始,我对编码很陌生。在 W3 上找到了一些代码用作我的基线。

  <button onclick="myFunction()" class="dropbtn">Dropdown</button>
  <div id="myDropdown" class="dropdown-content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div>

Javascript

toggle between hiding and showing the dropdown content */
function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {
    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}

【问题讨论】:

    标签: javascript html drop-down-menu dropdown submenu


    【解决方案1】:

    这是一种无需使用 JavaScript 只需 html 和 CSS 即可创建下拉菜单并使用 JavaScript 处理名称更改事件的方法

    需要注意的重要一点是,在使用

    设置新名称时
    title.innerHTML = name ;
    

    它会破坏原始列表并覆盖它,因此在该语句之后我们必须用

    重新添加列表
    title.appendChild(list)
    

    这是工作代码...

    var title = document.getElementById('title');
    var list = document.getElementById('list');
    
    let c1 = document.getElementById('c1');
    let c2 = document.getElementById('c2');
    let c3 = document.getElementById('c3');
    
    var namec1 = c1.innerHTML; 
    var namec2 = c2.innerHTML; 
    var namec3 = c3.innerHTML; 
    
    //console.log(name)
    c1.addEventListener("click", function(e){
    
    renameTitle(namec1)
    // Add more event stuff here
    
    });
    
    
    c2.addEventListener("click", function(e){
    renameTitle(namec2)
    // Add more event stuff here
    
    });
    
    
    c3.addEventListener("click", function(e){
    renameTitle(namec3)
    // Add more event stuff here
    
    });
    
    function renameTitle(name)
    {
      var title = document.getElementById('title');
      title.innerHTML = name ;
      title.appendChild(list)
    }
    a {
      text-decoration: none;
    }
    
    nav {
        font-family: monospace;
    }
    
    ul {
      background: darkorange;
        list-style: none;
        margin: 0;
        padding-left: 0;
    }
    
    li {
        color: #fff;
      background: darkorange;
        display: block;
        float: left;
        padding: 1rem;
        position: relative;
        text-decoration: none;
      transition-duration: 0.5s;
    }
      
    li a {
      color: #fff;
    }
    
    li:hover,
    li:focus-within {
        background: red;
        cursor: pointer;
    }
    
    li:focus-within a {
      outline: none;
    }
    
    ul li ul {
        background: orange;
        visibility: hidden;
      opacity: 0;
      min-width: 5rem;
        position: absolute;
      transition: all 0.5s ease;
      margin-top: 1rem;
        left: 0;
      display: none;
    }
    
    ul li:hover > ul,
    ul li:focus-within > ul,
    ul li ul:hover,
    ul li ul:focus {
      visibility: visible;
      opacity: 1;
      display: block
    }
    
    ul li ul li {
        clear: both;
      width: 100%;
    }
    <nav role="navigation">
      <ul>
      <li id='title'> Categories
          <ul class="dropdown" id='list'>
            <li id='c1'>Sub-1</li>
            <li id='c2'>Sub-2</li>
            <li id='c3'>Sub-3</li>
          </ul>
        </li>
        
    
          
      </ul>
    </nav>

    一个和你的cmets的一部分创建一个categories元素并将它添加到列表中添加

    var newItem = document.createElement('li');
    newItem .innerHTML = 'catogories';
    
    var list = document.getElementById("list");    // Get the <ul> element to insert a new node
    list.insertBefore(newItem, list.childNodes[0]);  // Insert <li> before the first child of <ul>
        <nav role="navigation">
          <ul>
          <li id='title'> Categories
              <ul class="dropdown" id='list'>
                <li id='c1'>Sub-1</li>
                <li id='c2'>Sub-2</li>
                <li id='c3'>Sub-3</li>
              </ul>
            </li>
            
    
              
          </ul>
        </nav>

    【讨论】:

    • 非常感谢!这很有帮助。有没有办法在下拉菜单中保留类别标题?例如,您单击“sub-3”,标题现在是“sub-3”,但当您再次单击下拉菜单时,它会在列表顶部显示标题类别?
    • 是的,您可以在我原始答案的末尾添加一个 sn-p,以向您展示如何将逻辑添加到原始函数中
    猜你喜欢
    • 2018-03-07
    • 2016-06-19
    • 2019-02-19
    • 1970-01-01
    • 2022-11-12
    • 1970-01-01
    • 2012-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多