我正在试图弄清楚这是否可能,我有一个类似下面标记的设置:
<div class="price-wrap">£12.95</div>
<div class="price-wrap">from £9.95</div>
<div class="price-wrap">£6.95</div>
<div class="price-wrap">£16.95</div>
<div class="price-wrap">£11.95</div>
<div class="price-wrap">from £1.95</div>
但我最理想的是,如果可能的话,就是这样:
<div class="price-wrap">£<span class="price">12.95</span></div>
<div class="price-wrap">from £<span class="price">9.95</span></div>
<div class="price-wrap">£<span class="price">6.95</span></div>
<div class="price-wrap">£<span class="price">16.95</span></div>
<div class="price-wrap">£<span class="price">11.95</span></div>
<div class="price-wrap">from £<span class="price">1.95</span></div>
因此,只需将数字包含在span
标记中,其中price
类不包括任何£
或from
等等。
任何建议将不胜感激!
答案 0 :(得分:3)
这应该做:
$('.price-wrap').each(function() {
var $this = $(this);
$this.html($this.html().replace(/([0-9.]+)/g, '<span>$1</span>'));
});
答案 1 :(得分:2)
您可以使用.html()中的回调迭代每个div的内容:
$('div.price-wrap').html(function () {
var val = $(this).text().split('£');
return val[0] + '£<span class="price">' + val[1] + '</span>'
})
.price {
color:green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="price-wrap">£12.95</div>
<div class="price-wrap">from £9.95</div>
<div class="price-wrap">£6.95</div>
<div class="price-wrap">£16.95</div>
<div class="price-wrap">£11.95</div>
<div class="price-wrap">from £1.95</div>
答案 2 :(得分:0)
也许你可以使用正则表达式并用<span>float</span>
正则表达式匹配浮动([-+]?[0-9]*\.?[0-9]+)