この HTML を考えると:
<div id="foo">
<input type=button class="foo abtn_1">
<input type=button class="joe bbtn_2">
<input type=button class="doe cbtn_2">
<input type=button class="joe dbtn_1">
<input type=button class="foo ebtn_2">
</div>
クリックすると、アンダースコアと数字でクラスの最初の部分を取得したいと思います。
したがって、最初の入力から次のようになります: abtn
現在私は使用しています:
$('#foo input').on('click', function () {
var a = $(this).attr('class')
.replace('foo','')
.replace('joe','')
.replace('doe','')
.replace('_1','')
.replace('_2','')
console.log(a);
});
おそらく正規表現を使用して、これを行うためのより堅牢で高速なパフォーマンス上の方法があるはずだと思いますか?