我有以下HTML:
<div>
<i class="fa fa-male ">:Before</i>
<i class="fa fa-like">:Before</i>
</div>
现在我想创建一个这样的字符串:male, like
。我怎么能这样做?
console.log(/fa-([^"]+)/.exec($('div').html()));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<i class="fa fa-male ">:Before</i>
<i class="fa fa-like">:Before</i>
</div>
答案 0 :(得分:6)
选择所有图标后,您需要循环播放。例如:
var str = $('.fa').map(function() {
return this.className.match(/fa-([^"]+)/)[1]
}).get()
.join(', ')
console.log(str)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<i class="fa fa-male">:Before</i>
<i class="fa fa-like">:Before</i>
</div>
答案 1 :(得分:0)
使用map
简单地遍历元素并提取(替换)元素的类名。
var icons = document.getElementsByClassName('fa');
var str = Array.from(icons)
.map(c => c.classList.value.replace('fa fa-', ''))
.join(', ')
console.log(str)
&#13;
<div>
<i class="fa fa-male">:Before</i>
<i class="fa fa-like">:Before</i>
</div>
&#13;