简体   繁体   中英

how to select all divs with the same class

I have several hidden divs on a page , some of them having the same class.

<div class="chapter-1"></div>
<div class="chapter-1"></div>

I prefix my class that way. How do I make all of them to display? I have tried

var id = 1; // get this from other source
$('.chapter-' + id).each().show();

you don't need the each(). you can just do

$(".classname").show();

so in your case (this will show all elements with class chapter-1).

var id = 1;
$(".chapter-"+id).show();

if you wish to show every div element with a class that starts with 'chapter-' you can use this :

$('div[class|="chapter"]').show();

$('.chapter-1').show(); or $('.chapter-1, .chapter-2').show();

Or, if they don't have a unique class, make them all have a unique class. Such as <div class="chapter-1 showDiv"></div> then:

$('.showDiv').show();

$.('[class ^= "classname"]')

那应该做

要跟踪Tims答案,如果您将类名称用于其他html实体,则还可以将其扩展为仅选择div。

$('div.classname' + id).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