如何获取从给定字符串开始的特定类的元素? [英] How to get elements of specific class starting with a given string?

查看:101
本文介绍了如何获取从给定字符串开始的特定类的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有多个类的元素的列表,例如:

I have a list of elements that have multiple classes, for example:

<input class="etape btn-info others">
<input class="etape btn-inverse others">
<input class="etape btn-danger others">

如何写jQuery代码,将允许我以下...

How to write jQuery-code that will allow me the following...

$(".etape").click(function(){
   $(this).get("the class that starts with btn-")
   // in order to store this value in a variable to reuse it later 
});


推荐答案

您可以使用正则表达式或 split 类名。

You can use Regular Expression or split the class name.

$(".etape").click(function(){
   var get = $.grep(this.className.split(" "), function(v, i){
       return v.indexOf('btn') === 0;
   }).join();
});

http:// jsfiddle.net/LQPh6/

这篇关于如何获取从给定字符串开始的特定类的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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