我有以下代码:
<div>December Friday the 12th</div>
我需要将其更改为:12月12日星期五
这是动态生成的,但格式是一致的。我无法编辑标记。如何用jQuery移动这些单词?
答案 0 :(得分:2)
您可以split()
完成。使用split()
将文本转换为数组,然后按数组索引格式化。示例:
var val = $('div').text().split(' '),
newText = val[1] + ', ' + val[0] + ' ' + val[3];
$('div').text(newText);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<div>December Friday the 12th</div>
答案 1 :(得分:1)
您可以使用.split()
和.text()
。
$('div').text(function (index, txt) {
var arr = txt.split(' ');
return arr[1] + ', ' + arr[0] + ' ' + arr[3];
});