问题
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