简体   繁体   中英

click button show this button number div JQUERY JAVASCRIPT

i have buttons witch has the same clasess. i want to when user click second button show second div, but buttons and divs have the same classes. here is my code.

html:

 <button class="test">1</button>
 <button class="test">2</button>
 <button class="test">3</button>

 <div class="divs">1</div>
 <div class="divs">2</div>
 <div class="divs">3</div>

The most suitable way would be to use id's but there is definitely an alternative way.

 $('.test').click(function() { let btnIndex = $(this).index(); $('.divs').hide(); $('.divs').eq(btnIndex).show(); })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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