JQuery单击附加到每个列表项的函数
我想在HTML文档主体的旁边区域添加三个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>
</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>
我写的代码是
</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());
});
})
现在,代码创建了s开关
当我特别想要的时候 那是什么时候 单击Hey1 ,然后仅显示具有
当我特别想要的时候 那是什么时候 单击
我就是那个
将显示和隐藏
我非常感谢您的帮助,因为您将单击事件附加到了一旁,而不是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>
我非常感谢您的帮助,因为您将单击事件附加到了一旁,而不是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();
});
});
});