使用jQuery删除逗号后的所有字符

时间:2016-04-19 07:09:12

标签: jquery

我想删除","之后的所有字符。在使用jQuery的字符串中,通过定位元素的类。我发现的其他解决方案是使用javascript,目标是字符串本身而不是类。

我的代码是:

<td class="priceinfo">€779,34&nbsp;</td>

如何删除&#34;,&#34;之后的字符?在td元素的文本中?

5 个答案:

答案 0 :(得分:3)

您可以使用jQuery的text()方法来实现此目的:

$('.priceinfo').text(function(i, t) {
    return t.substr(0, t.lastIndexOf(','));
});

Working example

答案 1 :(得分:3)

use below code

$('.priceinfo').html().split(",")[0]


below is the working fiddle

https://jsfiddle.net/5fwyjp52/1/

答案 2 :(得分:0)

你可以试试这个:

str = str.substr(0, str.lastIndexOf(","));

答案 3 :(得分:0)

这样做可以在第一个逗号后删除所有字符:

$(function(){
    $('.priceinfo').each(function(){
      text = $(this).text();
      textSplit = text.split(',');
      $(this).text(textSplit[0]);
  });
});

See it here

答案 4 :(得分:0)

您不必使用坦克敲钉,只需使用 jQuery-free ™解决方案:

[].forEach.call(document.querySelectorAll('.priceinfo'), function (el) {
    var h = el.innerHTML;
    el.innerHTML = h.substr(0, h.indexOf(','));
});