得到“班级”对象的属性拆分为字符串数组 [英] Get the "class" attribute of an object split into an array of strings

查看:106
本文介绍了得到“班级”对象的属性拆分为字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

序言:我是意大利人,抱歉我的英语不好。

Preamble: I'm Italian, sorry for my bad English.

所以这就是问题:

考虑这样的HTML对象:

considering an HTML object like this:

<div id="myDiv" class="c1 c2 c3 c4 c5"></div>

如何使用jquery从class属性获取字符串数组divelement?

how can I get an array of strings, using jquery, from the "class" attribute of the "div" element?

示例:

var a = $('#myDiv').getClassArray(); //Not working,just an example

return a;  //[c1,c2,c3,c4,c5]

我以为我能看到班级属性,然后拆分字符串,但它似乎太冗长,我想知道是否有一个更短更优雅的方法。

I thought I could read the "class" attribute and then split the string, but it seems too verbose, I wondered if there was a shorter and more elegant method.

推荐答案

您需要来获取身份证选择器并使用 split()从字符串中获取数组。

You need # for id selector and use split() to get array from string.

现场演示

Live Demo

var a = $('#myDiv').attr('class').split(' ');

您可以使用jQuery迭代数组 jQuery.each()

You can iterate through array using jQuery jQuery.each() of for loop.

var a = $('#myDiv').attr('class').split(' ');

$.each(a, function(index, item){
    alert(item);
});​

这篇关于得到“班级”对象的属性拆分为字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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