我试图以字符串中的每一个空格和连字符为目标,并用跨度将每个单词包装起来。 这是我正在尝试的,显然不是实现这一点的最好方法。 有什么想法吗?
$('.whatsOn').each(function() {
var date = $(this).find(".time");
var words = date.text().split(" ");
date.empty();
$.each(words, function(i, v) {
date.append($("<span>").text(v));
});
var wordsSpan = $(this).find(".time span").text().split("-");
$(this).find(".time span").empty();
$.each(words, function(i, v) {
$(this).find(".time span").append($("<span>").text(v));
});
});
<div class="whatsOn">
<dd class="time">14th-29th July</dd>
</div>
可以像这样将正则表达式传递给split。 date.text().split(/[-]/);
null
$('.whatsOn').each(function() {
var date = $(this).find(".time");
var words = date.text().split(/[- ]/);
console.log(words)
date.empty();
$.each(words, function(i, v) {
date.append($("<span>").text(v));
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<dl class="whatsOn">
<dd class="time">14th-29th July</dd>
</dl>