在不使用类的情况下计算div

osh*_*nen 1 jquery jquery-selectors

我试图在不使用类名的情况下计算组中的给定数量:

<div class="item" id="home1">home 1 text here</div>
<div class="item" id="home2">home 2 text here</div>
<div class="item" id="home3">home 3 text here</div>
<div class="item" id="home4">home 4 text here</div>
Run Code Online (Sandbox Code Playgroud)

我试过这个

alert($(['id^="home"']).length);
Run Code Online (Sandbox Code Playgroud)

但它一直给我0

有没有更合适的方法呢?

red*_*d-X 6

语法错误,试试这个:

alert($('[id^="home"]').length);
Run Code Online (Sandbox Code Playgroud)