javascript jQuery 获取特定的类名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6104048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery get specific class name
提问by Pinkie
I have a div with few classes in no specific order.
我有一个没有特定顺序的几个类的 div。
<div class="whatever styledark myclass"></div>
I'm interested in the styledark class. This can be styledark, stylewhite, stylered.... I want to get this class name no matter what color comes after the word style. The question is, when i click on the div, how i do i alert this class name that start with the word style disregarding other classes in the div. The classes are in no specific order.
我对 styledark 类感兴趣。这可以是styledark、stylewhite、stylered....我想得到这个类名,不管样式这个词后面是什么颜色。问题是,当我点击 div 时,我如何提醒这个以 style 一词开头的类名,而忽略 div 中的其他类。这些类没有特定的顺序。
采纳答案by Hussein
This is a great question. Although it can be done using many of the answers mentioned here, this is a little awkward and costly performance wise. The way i would recommend you do it is put the style class last, that way with only one line of code you can output the class name you are after.
这是一个很好的问题。尽管可以使用此处提到的许多答案来完成,但这在性能方面有点笨拙且代价高昂。我建议您这样做的方法是将样式类放在最后,这样只需一行代码就可以输出您想要的类名。
$('element').attr('class').split(' ').pop();
回答by jAndy
$('div').click(function() {
var style = this.className.split(/\s/).filter(function( cn ) {
return cn.indexOf('style') === 0;
});
alert(style);
});
Note this code uses Array.prototype.filter
and .indexof()
, which is not available in old'ish browsers. To make this more generic, it would look like:
请注意,此代码使用Array.prototype.filter
和.indexof()
,这在旧式浏览器中不可用。为了使它更通用,它看起来像:
$('div').click(function() {
var name = '';
$.each(this.className.split(/\s/), function( _, cn ) {
if( cn.indexOf('style') === 0 ) { name = cn; return false; }
});
alert(name);
});
回答by Kai
I used to use the classList attribute, but unfortunately it seems that it's not in safari. :( The following is what I came up with, but the RegExp may need just a little tweaking.
以前用的classList属性,可惜safari里好像没有。:( 以下是我想出的,但 RegExp 可能需要稍微调整一下。
$('div.button').each(function() {
$m = this.className.match(/(^|\s)style\S+(\s|$)/);
if($m) {
$(this).bind('click', { colorName : $m[0] }, function(e) {
alert(e.data.colorName);
);
}
});
回答by gion_13
$('div').click(function(){
var c = $(this).attr('class').split(/\s+/);
for(var s in c)
if(c[s].indexOf('style') == 0)
alert(c[s]);
});
live example : http://jsfiddle.net/gion_13/rXkNy/1/
回答by Kon
Found here: http://forum.jquery.com/topic/jquery-how-to-pick-out-classes-that-start-with-a-particular-string
在这里找到:http: //forum.jquery.com/topic/jquery-how-to-pick-out-classes-that-start-with-a-particular-string
var icons = ["Star", "Plus", "Left", "Right", "Up", "Down"];
$("div.button").each(function() {
var $dv = $(this);
$.each(icons, function() {
if ($dv.hasClass("icon" + this)) {
$dv.append('<img src="Images/' + this + '.gif" alt="" />');
return false;
}
});
});
回答by roberkules
$("div[class]").click(function(e){
$.each(this.className.split(/\s/), function(i, val) {
if (/^style/.test(val)) {
alert(val.substr(5));
return false;
}
});
});
回答by Zach
Assuming you have the list of class names in a variable called "classes":
假设您在名为“classes”的变量中有类名列表:
classes = classes.split(" ");
for (var i=0; i<classes.length; i++)
{
var class = classes[i];
if (class.indexOf("style") == 0)
{
alert(class);
}
}
回答by Travis Heeter
$('element').attr("class").split(" ").filter(getStyleClass);
function getStyleClass(value) {
return value.indexOf("style") > -1;
}
And to alert the 'style' class on a click, as you specified:
并按照您的指定在单击时提醒“样式”类:
$('element').click(function()[
var style_class = $(this).attr("class").split(" ").filter(getStyleClass);
alert(style_class);
}
// include the same getStyleClass from above
more depth:
更深入:
$('element').attr("class").split(" ")
is a good start - it puts the classes in an array, but .pop()
simply removes the last element of the array, and that does not solve the problem.
$('element').attr("class").split(" ")
是一个好的开始——它将类放在一个数组中,但.pop()
只是删除了数组的最后一个元素,这并不能解决问题。
.filter()
is a great solution because you can write a small function that decides which elements you want.
.filter()
是一个很好的解决方案,因为您可以编写一个小函数来决定您想要哪些元素。
In our case we only want elements of the array that have 'style'. We can use .indexOf()
to decide if the string contains "style" like this: value.indexOf("style") > -1
.
在我们的例子中,我们只需要具有“样式”的数组元素。我们可以使用.indexOf()
,以决定是否字符串包含“风格”是这样的:value.indexOf("style") > -1
。