将某个类的所有元素的ID放入数组 [英] Getting ID of all elements of a certain class into an array

查看:94
本文介绍了将某个类的所有元素的ID放入数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要做的:

当前,我正在使用它来创建一个与.cookie类名称匹配的所有元素的数组.现在,我正在获取该元素的文本值,这不是我所需要的:

Currently I am using this to create an array of all elements matching the class name of .cookie. Right now I am getting the text value of that element, which is not what I need:

var getAllCookies = $('.cookie').text();
var cookiesArray = jQuery.makeArray(getAllCookies);
alert(cookiesArray[0]);

我需要的是找到某个类(.cookie)的所有元素,获取该元素的ID值并将该ID值存储在数组中.

What I need is to find all elements of a certain class (.cookie), get that element's ID value and store that ID value inside of array.

推荐答案

我认为这应该可以满足您的要求:

I think this should do what you're after:

var ids = $('.cookie').map(function(index) {
    // this callback function will be called once for each matching element
    return this.id; 
});

map 的文档.

这是一个有效的jsFiddle演示.

这篇关于将某个类的所有元素的ID放入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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