[英]jQuery: show onclick works only for first button
我希望有更多按钮显示内容onclick。 当有人单击任何按钮时,可能会显示所有内容(toShow)。 目前它可以工作,但只在第一个按钮。 因此,当有人点击第一个按钮时,会显示所有内容,但我需要在有人点击页面上的任何按钮时显示所有内容。 也许我需要循环? 感谢帮助。
$(document).ready(function(){
$("toShow").hide();
$("#show").click(function(){
$("toShow").show();
$("buttonShow").hide();
});
});
<h1>First element</h1>
<buttonShow>
<button id="show">show</button>
</buttonShow>
<toShow>
content
</toShow>
<h2>Second element</h2>
<buttonShow>
<button id="show">show</button>
</buttonShow>
<toShow>
content
</toShow>
这是因为你有多个相同的id。 改为分配类,对于任何具有类.show
按钮 - 将触发相应的事件处理程序
<button class="show">show</button>
<button class="show">show</button>
<button class="show">show</button>
$('.show').click(function(){
$('toShow').show();
$('buttonShow').hide();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.