jquery select and use part of class name

别来无恙 提交于 2019-12-13 03:55:20

问题


I have a lot of classes who starts with edit_. I've styled them with [class^="edit_"]. Now, each class have a different number like edit_20 edit_80 edit_150. How can i strip the first part of the class name (edit_) so that if passing it to a variable, it will only contain the number?


回答1:


Try to .split() it out with _ and get the second element from the returned array.,

var number = "edit_20".split('_')[1];



回答2:


Simple replace can do but make sure to convert into int.

 var number = parseInt(str.replace("edit_",''));



回答3:


One way to do it with Regex:

 if(/^edit_(\d*)/.test('edit_20'))
      var num = (/^edit_(\d*)/.exec('edit_20'))[1];



回答4:


ok, for those who will be landing here...i solved this way:

$("[class^='edit_']").each( function() {
    var ml = $(this).attr('class').split(' ')[0].split('_')[1];
    $(this).editable('[url]', {
        [...]
        maxlength   : ml,
    });
});


来源:https://stackoverflow.com/questions/24242938/jquery-select-and-use-part-of-class-name

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