从Jquery中的字符串中删除所有逗号

时间:2021-10-28 21:35:53

I have many classes and want to remove the commas in them if any commas exist there.

我有很多课程,如果有任何逗号,我想删除它们中的逗号。

I wrote the below code but the code does not work correctly. The second class value is replaced with the first value.

我写了下面的代码,但代码无法正常工作。第二个类值替换为第一个值。

   
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>

3 个解决方案

#1


7  

Try this. Updated your code:

尝试这个。更新了您的代码:

   

$(".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>

#2


3  

I would iterate over each element and change its' text:

我会迭代每个元素并更改其'文本:

var removebox = $(".remove");

removebox.each(function () {

  var oldtext = $(this).text();

  $(this).text(oldtext.replace(',', ''));

});

#3


1  

While you've already accepted an answer to this problem it's worth pointing out that they're both rather too verbose, and one answer, shennan's (at the time of writing), will only remove one comma from each of the given elements should multiple commas be present therein.

虽然你已经接受了这个问题的答案,但值得指出的是,它们都太冗长了,而且一个答案,shennan(在撰写本文时),只会删除每个给定元素中的一个逗号逗号在其中。

That said, a more concise version is below:

也就是说,更简洁的版本如下:

// 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>

Of course it's also worth showing that the above is perfectly possible in plain JavaScript, and still relatively concise:

当然,值得表明上述内容在纯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>

References:

参考文献:

#1


7  

Try this. Updated your code:

尝试这个。更新了您的代码:

   

$(".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>

#2


3  

I would iterate over each element and change its' text:

我会迭代每个元素并更改其'文本:

var removebox = $(".remove");

removebox.each(function () {

  var oldtext = $(this).text();

  $(this).text(oldtext.replace(',', ''));

});

#3


1  

While you've already accepted an answer to this problem it's worth pointing out that they're both rather too verbose, and one answer, shennan's (at the time of writing), will only remove one comma from each of the given elements should multiple commas be present therein.

虽然你已经接受了这个问题的答案,但值得指出的是,它们都太冗长了,而且一个答案,shennan(在撰写本文时),只会删除每个给定元素中的一个逗号逗号在其中。

That said, a more concise version is below:

也就是说,更简洁的版本如下:

// 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>

Of course it's also worth showing that the above is perfectly possible in plain JavaScript, and still relatively concise:

当然,值得表明上述内容在纯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>

References:

参考文献: