JavaScript / jQuery:替换部分字符串? [英] JavaScript/jQuery: replace part of string?
本文介绍了JavaScript / jQuery:替换部分字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用这样的文字:
<div class="element">
<span>N/A, Category</span>
</div>
我想摆脱每次出现的 N / A
。
这是我的尝试:
$('.element span').each(function() {
console.log($(this).text());
$(this).text().replace('N/A, ', '');
});
记录的文本是范围内的文本,因此选择器没问题。
The logged text is the text inside of the span so the selector is okay.
我在这里做错了什么?
推荐答案
你需要在之后设置文本替换呼叫:
You need to set the text after the replace call:
$('.element span').each(function() {
console.log($(this).text());
var text = $(this).text().replace('N/A, ', '');
$(this).text(text);
});
这是你的代码: http://jsfiddle.net/ZSXb6/
这是另一个酷你可以做到这一点(帽子提示@Felix King):
Here's another cool way you can do it (hat tip @Felix King):
$(".element span").text(function(index, text) {
return text.replace("N/A, ", "");
});
这篇关于JavaScript / jQuery:替换部分字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文