![](/img/trans.png)
[英]Remove all parentheses, commas etc from a string and then extract the first 5 words
[英]Remove all commas from a string in Jquery
我有很多课程,如果有任何逗号,我想删除它们中的逗号。
我写了下面的代码,但代码无法正常工作。 第二个类值替换为第一个值。
var removebox = $(".remove"), removeboxHtml = removebox.html(); removebox.html(removeboxHtml.replace(/,/g , ''));
<html> <head> <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js"></script> </head> <body> <span class="remove">,17500000</span> <span class="remove">,2479000</span> </body> </html>
尝试这个。 更新了您的代码:
$(".remove").each(function(){ $(this).html($(this).html().replace(/,/g , '')); });
<html> <head> <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js"></script> </head> <body> <span class="remove">,17500000</span> <span class="remove">,2479000</span> </body> </html>
我会迭代每个元素并更改其'文本:
var removebox = $(".remove");
removebox.each(function () {
var oldtext = $(this).text();
$(this).text(oldtext.replace(',', ''));
});
虽然你已经接受了这个问题的答案,但值得指出的是它们都太冗长了,而且一个答案, shennan (在撰写本文时),只会删除每个给定元素中的一个逗号逗号在其中。
也就是说,更简洁的版本如下:
// select the elements to update:
$('.remove')
// we then use the text method's anonymous function,
// using the two arguments:
// index: the index of the current element in the collection,
// currentText: the text of the current element of the
// collection over the text method iterates:
.text(function(index, currentText) {
// here we access the currentText variable and use
// String.prototype.replace(), with a regular literal (/.../)
// to remove all occurrences (g) of the specified comma character
// replacing those occurrences with an empty string (''); this
// is comma-removed string is returned to the text method in
// order to update the text of each element as required:
return currentText.replace(/,/g, '');
});
$('.remove').text(function(index, currentText) { return currentText.replace(/,/g, ''); });
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js"></script> <span class="remove">,17500000</span> <span class="remove">,2479000</span> <span class="remove">5,279,000</span>
当然,值得表明上述内容在纯JavaScript中完全可行,并且仍然相对简洁:
// we use document.querySelectorAll() to retrieve
// a non-live HTMLCollection of elements matched
// by the supplied CSS selector:
var elements = document.querySelectorAll('.remove');
// we use Array.from() to convert an Array-like Object
// into an Array, in order that we can then iterate
// over those elements using Array.prototype.forEach():
Array.from(elements).forEach(function(elem) {
// 'elem', the first argument, is a reference to the
// current element of the Array of elements over
// which we're iterating.
// here we retrieve the current text-content of the
// current element, and use String.prototype.replace(),
// with a regular expression (exactly as above) to
// replace all occurrences ('g') of the comma character
// (',') in the string supplied by elem.textContent and
// we replace those commas with a empty string (''):
elem.textContent = elem.textContent.replace(/,/g, '');
});
var elements = document.querySelectorAll('.remove'); Array.from(elements).forEach(function(elem) { elem.textContent = elem.textContent.replace(/,/g, ''); });
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js"></script> <span class="remove">,17500000</span> <span class="remove">,2479000</span> <span class="remove">5,279,000</span>
参考文献:
text()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.