简体   繁体   English

jQuery:show onclick仅适用于第一个按钮

[英]jQuery: show onclick works only for first button

I want to have more buttons what will show content onclick. 我希望有更多按钮显示内容onclick。 When someone click any button, all contents (toShow) may appear. 当有人单击任何按钮时,可能会显示所有内容(toShow)。 Currently it works but only on first button. 目前它可以工作,但只在第一个按钮。 So when someone click first button, all contents appear, but I need that all contents appear when someone click to any button on page. 因此,当有人点击第一个按钮时,会显示所有内容,但我需要在有人点击页面上的任何按钮时显示所有内容。 Maybe I need for cycle? 也许我需要循环? Thanks for help. 感谢帮助。

$(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>

This is because you have multiple of the same id's. 这是因为你有多个相同的id。 Assign classes instead, and for any button with class .show - the appropriate event handler will fire 改为分配类,对于任何具有类.show按钮 - 将触发相应的事件处理程序

<button class="show">show</button>
<button class="show">show</button>
<button class="show">show</button>

$('.show').click(function(){
    $('toShow').show();
    $('buttonShow').hide();
});

JSFiddle Example JSFiddle示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM