JQuery单击附加到每个列表项的函数

JQuery单击附加到每个列表项的函数,jquery,Jquery,我想在HTML文档主体的旁边区域添加三个 Hey1 Hey2 Hey3 每个按钮都附带了一个点击切换功能,以便显示和隐藏 HTML正文中的适当div。 </aside> <div id="id1" > I am the one </div> <div id="id2" > I am the second</div> <div id="id3" > I am the third</div>

我想在HTML文档主体的旁边区域添加三个
  • Hey1
  • Hey2
  • Hey3
  • 每个按钮都附带了一个点击切换功能,以便显示和隐藏 HTML正文中的适当div。

       </aside>
      <div id="id1" > I am the  one </div>
      <div id="id2" > I am the  second</div>
      <div id="id3" > I am the  third</div>
      </body>
    
    
    我就是那个
    我是第二个
    我是第三个
    
    例如,
  • Hey1
  • 附加的单击功能将仅使用id=“id1”切换div 等等

       </aside>
      <div id="id1" > I am the  one </div>
      <div id="id2" > I am the  second</div>
      <div id="id3" > I am the  third</div>
      </body>
    
    我写的代码是

       </aside>
      <div id="id1" > I am the  one </div>
      <div id="id2" > I am the  second</div>
      <div id="id3" > I am the  third</div>
      </body>
    
       $(document).ready(function() {   $("div").hide();    
       jQuery.each([1,2,3], function(n,value){ n++; 
    
        $('aside').append("<li> hey"+value+" </li>").click(function(){$('#id'+value).toggle()});
        });
        })
    
    $(document.ready(function(){$(“div”).hide();
    each([1,2,3],函数(n,值){n++;
    $('aside')。追加(“
  • hey”+value+”
  • ”)。单击(函数(){$('id'+value.toggle()); }); })
    现在,代码创建了
  • 的OK,并附加了click函数。 问题是我点击了
  • 中的任何一个 所有三个
    s开关
    当我特别想要的时候 那是什么时候 单击
  • Hey1
  • ,然后仅显示具有
    我就是那个 将显示和隐藏

       </aside>
      <div id="id1" > I am the  one </div>
      <div id="id2" > I am the  second</div>
      <div id="id3" > I am the  third</div>
      </body>
    

    我非常感谢您的帮助,因为您将单击事件附加到了一旁,而不是li

       </aside>
      <div id="id1" > I am the  one </div>
      <div id="id2" > I am the  second</div>
      <div id="id3" > I am the  third</div>
      </body>
    
    $(document).ready(function() {   $("div").hide();    
        $.each([1,2,3], function(n,value){
            n++; 
            $li = $("<li> hey"+value+" </li>").click(function(){
                $('#id'+value).toggle();
            });
            $('aside').append($li);
        });
    })
    
    $(document.ready(function(){$(“div”).hide();
    $。每个([1,2,3],函数(n,值){
    n++;
    $li=$(“
  • 嘿”+value+“
  • ”)。单击(函数(){ $('#id'+值).toggle(); }); $('aside')。追加($li); }); })
    您没有将单击处理程序附加到
    li
    ,而是附加到
    旁边的
    ,请执行以下操作:

       </aside>
      <div id="id1" > I am the  one </div>
      <div id="id2" > I am the  second</div>
      <div id="id3" > I am the  third</div>
      </body>
    
    $(document).ready(function() {   
        $("div").hide();    
        $.each([1,2,3], function(n,value){
          $('<li />', {text: 'hey'+value}).appendTo('aside').on('click', function() {
              $('#id'+value).toggle();
          });
        });
    });
    
    $(文档).ready(函数(){
    $(“div”).hide();
    $。每个([1,2,3],函数(n,值){
    $('
  • ',{text:'hey'+value}).appendTo('aside').on('click',function(){ $('#id'+值).toggle(); }); }); });