我想要更多的按钮,它们将在点击时显示内容。当有人点击任何按钮时,所有内容 (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();
});
关于javascript - jQuery:显示 onclick 仅适用于第一个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30132474/