如何在jQuery中选择具有特定ID的所有元素? [英] How to select all elements with a particular ID in jQuery?

查看:421
本文介绍了如何在jQuery中选择具有特定ID的所有元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图选择所有< div> 在jQuery中使用相同的ID。

I'm trying to select all <div>s with the same ID in jQuery. How do i do it?

我试过了,但没有用。

jQuery('#xx').each(function(ind,obj){
      //do stuff;
});


推荐答案

我会使用不同的ID,类。

I would use Different IDs but assign each DIV the same class.

<div id="c-1" class="countdown"></div>
<div id="c-2" class="countdown"></div>

这也有额外的好处是能够根据返回的jQuery '.countdown')。length

This also has the added benefit of being able to reconstruct the IDs based off of the return of jQuery('.countdown').length

确定在每个倒计时器中添加多个类。 IE:

Ok what about adding multiple classes to each countdown timer. IE:

<div class="countdown c-1"></div>
<div class="countdown c-2"></div>
<div class="countdown c-1"></div>

这样你就能获得最好的两个世界。它甚至允许重复IDS

That way you get the best of both worlds. It even allows repeat 'IDS'

这篇关于如何在jQuery中选择具有特定ID的所有元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆