Find contains class name in multiple classes

只愿长相守 提交于 2019-12-21 21:17:23

问题


I want to get the id in class like...

<div class="main demo id_111 test"></div>

<div class="main demo test id_222 test demo"></div>

<div class="id_3 main demo test  test demo"></div>

Result must be 111 , 222 and 3

So I code like this

var id = $(".main").attr("class");
var id = id.split("id_");
var id = id[1].split(" ");
var id = id[0];

$("body").append(id);

But someone know better coding than mine ?

Playground : http://jsfiddle.net/UHyaD/


回答1:


You can use a regex :

var id = $(".main").attr("class").match(/id_(\d*)/)[1];

JSfiddle : http://jsfiddle.net/scaillerie/UHyaD/1/




回答2:


You can use match function:

var id = $(".main").attr("class").match(/\d+/g).join('');


来源:https://stackoverflow.com/questions/13877393/find-contains-class-name-in-multiple-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!