代码之家  ›  专栏  ›  技术社区  ›  user8072194

其他命名按钮的方法?

  •  -2
  • user8072194  · 技术社区  · 7 年前

    我正在学习W3Schools按钮教程。他们的手风琴按钮示例:

    <button class="accordion">Section 2</button>
    <div class="panel" id="panel2">
      <p>Lorem ipsum...</p>
    </div>
    

    这可能吗?

    2 回复  |  直到 7 年前
        1
  •  0
  •   sauntimo    7 年前

    我写了一个函数,当你点击一个按钮时,你会添加一个新的部分——然而,你需要将你想要的值传递给这个函数——大概是从你的用户输入。

    function addSection( section_name, id ){
    
      var html = '<button class="accordion">' + section_name + '</button>'
        + '<div class="panel" id="panel' + id + '">'
        + '<p>Lorem ipsum...</p>'
        + '</div>'
    
      document.getElementsByClassName( 'content' )[0].innerHTML += html;
    
    }
    
    document.getElementById( 'add').addEventListener( 'click', function(){
      addSection( 'Section 2', 2 );
    });
    #add {
      margin-bottom: 20px;
    }
    <button id="add">Add second section</button>
    
    <div class="content">
    
      <button class="accordion">Section 1</button>
      <div class="panel" id="panel1">
        <p>Lorem ipsum...</p>
      </div>
      
    </div>
        2
  •  -1
  •   Luple    7 年前

    尝试使用“.html('此处显示新文本');”

    如果您不确定如何使用jquery设置属性,那么在您的情况下应该是:

    $(“.accordian”)。html('此处显示新文本');