我需要<li>用句点替换每个值中的逗号.
我不知道我的代码有什么问题.我检查了控制台,但没有...
$('#stats li').each(function () {
$(this).text().replace(/,/g, '.');
});
Run Code Online (Sandbox Code Playgroud)
此代码应针对每个<li>在<ul id="stats">.然后它应该替换每个,中的每个<li>并用a替换它.
我也试过这个:
$('#stats li').each(function () {
var comma = /,/g;
if(comma.test($this)) {
$(this).replace(comma, '.');
}
});
Run Code Online (Sandbox Code Playgroud)
我试过这个:
$('#stats li').each(function () {
var stats = [];
stats.push($(this).text());
stats.replace(/,/g, '.');
console.log(stats);
});
Run Code Online (Sandbox Code Playgroud)
这是小提琴.
问题是该replace方法返回一个新字符串.它不会修改字符串.试试这个:
$('#stats li').each(function () {
$(this).text($(this).text().replace(/,/g, '.'));
});
Run Code Online (Sandbox Code Playgroud)
但就此而言,jQuery的text方法也接受了一个函数.这是一个很多清洁:
$('#stats li').text(function (index, text) {
return text.replace(/,/g, '.');
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1707 次 |
| 最近记录: |